Python统计列表中重复次数最多的前N个元素

from collections import Counter

a = [1, 1, 7, 3, 6, 2, 5, 4, 4, 3, 9, 4, 4, 1]

#统计列表中重复次数最多的前N个元素
N = 3

print(Counter(a).most_common(N))

 

#输出是[(4, 4), (1, 3), (3, 2)]

 

posted @ 2019-03-04 10:22  coffee~  阅读(4719)  评论(0编辑  收藏  举报