Python实战171203统计

统计序列中元素出现的频次

如何统计出某一个随机数列的元素出现的次数是多少?

import random
data=[random.randint(0,7) for _ in range(15)]
c=dict.fromkeys(data,0)
for x in data:
c[x]+=1
print(data,c)
print(c)

 print (sorted(c.items(),key=lambda c:c[1]))

import collections
c2 = collections.Counter(data)
print(c2)

 

 c3 = c2.most_common(3)
print(c3)

 

原文地址:https://www.cnblogs.com/3daytears/p/8066893.html