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)]
标签:eleCounts,Python,list,top,Counter,个数,most,common 来源: https://www.cnblogs.com/lucktomato/p/16323547.html