我是靠谱客的博主 老实小松鼠,这篇文章主要介绍学习笔记 | Counter找出列表中出现次数最多的元素,现在分享给大家,希望可以做个参考。

找出列表中出现次数最多的元素

  • 在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找出列表中出现次数最多的元素的全部内容,更多相关学习笔记内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(150)

评论列表共有 0 条评论

立即
投稿
返回
顶部