from random import randint
from collections import Counter
data = [randint(0 , 100) for _ in range(100)]
c = Counter(data) //统计每个元素出现的pinlv
c2 = c.most_common(3) //找出出现频率最高的3个元素
print(c2)
打印的结果:
Counter({6: 5, 84: 3, 85: 3, 35: 3, 38: 3, 36: 3, 69: 3, 54: 3, 86: 3, 65: 3, 26: 2, 78: 2, 87: 2, 27: 2, 47: 2, 15: 2, 76: 2, 49: 2, 88: 2, 48: 2, 100: 2, 9: 2, 20: 2, 5: 1, 45: 1, 98: 1, 77: 1, 99: 1, 59: 1, 75: 1, 96: 1, 29: 1, 74: 1, 68: 1, 42: 1, 0: 1, 41: 1, 24: 1, 7: 1, 56: 1, 80: 1, 89: 1, 18: 1, 92: 1, 64: 1, 31: 1, 8: 1, 55: 1, 58: 1, 90: 1, 95: 1, 30: 1, 19: 1, 25: 1, 37: 1, 60: 1, 34: 1, 51: 1, 39: 1, 44: 1, 67: 1, 61: 1, 32: 1, 1: 1, 94: 1})
[(6, 5), (84, 3), (85, 3)]