from collections import Counter
words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
word_counts = Counter(words)
# 找出出现次数最多的3个元素
print(word_counts.most_common(3))
# 打印结果为:
[('eyes', 8), ('the', 5), ('look', 4)]
Counter找出频率最高
最新推荐文章于 2023-03-07 15:24:27 发布
本文介绍了一种使用Python的collections模块中Counter类进行词频统计的方法。通过实例演示了如何统计一个列表中各单词出现的次数,并展示了如何找出出现频率最高的前几个元素。

913

被折叠的 条评论
为什么被折叠?



