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)

 

posted @ 2017-12-19 17:35  三天泪  阅读(132)  评论(0编辑  收藏  举报