找出列表中出现次数最多的元素
- 在Python程序中,如果想找出列表中出现次数最多的元素,可以考虑使用collections模块中的Counter类,调用Counter类中的函数most_common() 来实现上述功能。
from collections import Counter
words = [
'look', 'into', 'my', 'AAA', 'look', 'into', 'my', 'AAA',
'the', 'AAA', 'the', 'AAA', 'the', 'eyes', 'not', 'BBB', 'the',
'AAA', "don't", 'BBB', 'around', 'the', 'AAA', 'look', 'into',
'BBB', 'AAA', "BBB", 'under'
]
word_counts = Counter(words)
top_three = word_counts.most_common(3)
print(top_three)

最后
以上就是老实小松鼠最近收集整理的关于学习笔记 | Counter找出列表中出现次数最多的元素的全部内容,更多相关学习笔记内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复