from collections import Counter
import random
data = [random.randint(0,20) for _ in range(20)]
print('20个0-20之间的随机数:',data)
d = dict.fromkeys(data,0)#以data中出现的数字为键,0为值生成字典
print('以data中出现的数字为键,0为值生成字典:',d)
for x in data:
d[x]+=1
print('统计结果:',d)
d2 = Counter(data)
print('Counter统计结果:',d2)
print('统计出现频率最高的前3个数:',d2.most_common(3))
输出: