Python统计list中每个元素出现的个数

from collections import Counter

n = ['a','b','a','c','d','a']
# 统计list中每个元素出现的个数
eleCounts = Counter(n)
# most_common()返回出现次数排名前n个的元素,不输入时默认按照出现次数对所有数据排序
top_one = eleCounts.most_common(2)

print(top_one)
[('a', 3), ('b', 1)]

  

posted @ 2022-05-29 11:55  lucky_tomato  阅读(354)  评论(0编辑  收藏  举报