python 找出序列中出现次数最多的元素方法

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'
]
print (Counter(words))
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2, 'not': 1, "don't": 1, "you're": 1, 'under': 1})
print (Counter(words).most_common(4))
[('eyes', 8), ('the', 5), ('look', 4), ('into', 3)]

原文链接

posted @ 2022-08-19 22:59  luoganttcc  阅读(18)  评论(0编辑  收藏  举报