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)]
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)]