Search code examples
pythonpython-3.xlistsortingtext

How to sort text by how much it is repeated?


I have a text file abc.txt, It contains the following lines:

a
a
b
c
c
c
d
d

I want to sort this list by how much each word is repeated in descending order, in this case it would be:

c - 3 times
a - 2 times
d - 2 times
b - 1 time

So far, I have read the text file, tried to sort the list but failed using Python... Any help would be appreciated!


Solution

  • this code:

    • read lines from file
    • count them using collections.Counter which doing the sort for us as well
    • show them with the format you requested
    from collections import Counter
    
    
    def main():
        file_path = 'abc.txt'
    
        with open(file_path, 'r') as f:
            lines = f.read().split('\n')
    
        result = Counter(lines)
    
        for_show = '\n'.join(f'{key}: {value} item{"s" if value > 1 else ""}' for key, value in result.most_common())
    
        print(for_show)
    
    
    if __name__ == '__main__':
        main()