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()方法

你可能感兴趣的:(NLP,Python编程基础)