将文本文件排序到由模式确定的列表中
Sort a text file into a list determined by mode
我有一个文本文件:
hello my name is bill hello there hello there hiya hiya hiya
每个短语由四个空格分隔。我如何按频率对这些词(换行)进行排序。
感谢任何帮助。
您可以使用 collections.Counter
来实现。
from collections import Counter
with open("your file.txt", "r") as f:
phrases = Counter(f.read().split(" "))
for phrase, occurrences in sorted(phrases.items(), key=lambda _: _[1], reverse=True):
print "Phrase: {} -- Occurrences: {}".format(phrase, occurrences)
我有一个文本文件:
hello my name is bill hello there hello there hiya hiya hiya
每个短语由四个空格分隔。我如何按频率对这些词(换行)进行排序。
感谢任何帮助。
您可以使用 collections.Counter
来实现。
from collections import Counter
with open("your file.txt", "r") as f:
phrases = Counter(f.read().split(" "))
for phrase, occurrences in sorted(phrases.items(), key=lambda _: _[1], reverse=True):
print "Phrase: {} -- Occurrences: {}".format(phrase, occurrences)