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!
CodePudding user response:
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()
CodePudding user response:
Another approach can be:
with open("abc.txt", 'r') as f:
data = f.readlines()
counter = {}
for w in data:
w = w.strip()
counter[w]=counter.get(w, 0) 1
sorted_data = sorted(counter.items(), key=lambda x: x[1], reverse=True)
for data in sorted_data:
print (f'{data[0]}-{data[1]} times')
Output:
c-3 times
a-2 times
d-2 times
b-1 times