如何按重复次数对文本进行排序?
How to sort text by how much it is repeated?
我有一个文本文件abc.txt
,它包含以下几行:
a
a
b
c
c
c
d
d
我想按每个单词重复的次数降序排列此列表,在本例中为:
c - 3 times
a - 2 times
d - 2 times
b - 1 time
到目前为止,我已经阅读了文本文件,尝试对列表进行排序但使用 Python 失败...
如有任何帮助,我们将不胜感激!
此代码:
- 从文件中读取行
- 使用 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()
另一种方法可以是:
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
我有一个文本文件abc.txt
,它包含以下几行:
a
a
b
c
c
c
d
d
我想按每个单词重复的次数降序排列此列表,在本例中为:
c - 3 times
a - 2 times
d - 2 times
b - 1 time
到目前为止,我已经阅读了文本文件,尝试对列表进行排序但使用 Python 失败... 如有任何帮助,我们将不胜感激!
此代码:
- 从文件中读取行
- 使用 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()
另一种方法可以是:
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