python中的Counter.most_common()

源于: 执行类代码 – CountTxtClass.py – 函数plot_sample_num


栗子: 找出一个序列中出现次数最多的元素

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'
]
from collections import Counter
word_counts = Counter(words)
# 出现频率最高的3个单词
top_three = word_counts.most_common(3)
print(top_three)

结果为:

[('eyes', 8), ('the', 5), ('look', 4)]

学习链接:

Python中的Counter.most_common()方法

发布了255 篇原创文章 · 获赞 28 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43283397/article/details/104258268
今日推荐