【问题标题】:How to sort text by how much it is repeated? [closed]如何按重复次数对文本进行排序? [关闭]
【发布时间】:2021-11-25 10:27:50
【问题描述】:
我有一个文本文件abc.txt,它包含以下几行:
a
a
b
c
c
c
d
d
我想按每个单词按降序重复的次数对这个列表进行排序,在这种情况下是:
c - 3 times
a - 2 times
d - 2 times
b - 1 time
到目前为止,我已经阅读了文本文件,尝试对列表进行排序,但使用 Python 失败...
任何帮助将不胜感激!
【问题讨论】:
标签:
python
python-3.x
list
sorting
text
【解决方案1】:
这段代码:
- 从文件中读取行
- 使用 collections.Counter 对它们进行计数,它也为我们进行排序
- 以您要求的格式显示它们
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()
【解决方案2】:
另一种方法可以是:
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')
输出:
c-3 times
a-2 times
d-2 times
b-1 times