我必须使用python计算列表中的单词频率,但我想要的是我想根据它的出现来计算单词,但我不打算全部打印
例如,我有这个清单
lists = ["me","sleep","love","me","love","love","love","rain","love","me","me","rain","book","book","rain","book","catch","watch"]
如果我使用它:
from collections import Counter
counts = Counter(lists)
print(counts)
它会产生结果:
Counter({'love': 5, 'me': 4, 'rain': 3, 'book': 3, 'sleep': 1, 'catch': 1, 'watch': 1})
但我的预期结果是:
Sort by 4 words that have highest occurance
Love : 5
Me : 4
Rain : 3
Book : 3
所以"睡觉","赶上"和"观看"不会包含在我的结果中 如何修改我的代码,以便我的代码输出类似于我的预期结果,我的意思是按照出现最高值的XX字排序。
非常感谢
答案 0 :(得分:2)
from collections import Counter
counts = Counter(lists).most_common(4)
print ("Sort by 4 words that have highest occurance")
print ("\n".join([str(x)+ " : " + str(y) for x,y in counts]))
输出:
Sort by 4 words that have highest occurance
love : 5
me : 4
rain : 3
book : 3
sleep : 1
答案 1 :(得分:1)
如何修改我的代码,以便我的代码输出类似于我的预期结果
from collections import Counter
lists = ["me","sleep","love","me","love","love","love","rain","love",
"me","me","rain","book","book","rain","book","catch","watch"]
counts = Counter(lists).most_common(4)
print ("Sort by 4 words that have highest occurance")
for word, count in counts:
print("{} : {}".format(word.title(), count))
输出
Sort by 4 words that have highest occurance
Love : 5
Me : 4
Book : 3
Rain : 3
注意:没有为订购具有重复值的条目指定规则,例如Book
和Rain