from random import randint from mrjob.py2 import xrange data = [randint(0, 20) for _ in xrange(30)] print(data) # 统计每个数值出现的频率,第一种方式 c = dict.fromkeys(data, 0) print(c) for x in data: c[x] += 1 print(c) # 对字典里面的value进行倒序排列 res = sorted(c.items(), key=lambda kv: (kv[1]), reverse=True) print(res) # 第二种方式 from collections import Counter c2 = Counter(data) print(c2) # 统计出现最高的三个元素 print(c2.most_common(3)) # 词频统计 import re text = open('test.log').read() # print(text) c3 = Counter(re.split('\W+', text)) print(c3.most_common(10))