随机序列,找出出现次数最高的3个元素,输出出现次数
from random import randint
data = [randint(0, 20) for _ in xrange(30)]
print data
# data作为键, 0作为值
c = dict.fromkeys(data, 0)
for x in data:
c[x] += 1
print c
"""
使用collections.Counter对象
将序列传入Counter的构造器,得到对象是元素频率的字典
"""
from collections import Counter
c2 = Counter(data)
print c2
c2 = c2.most_common(3)
print c2
英文文章进行词频统计,找出现次数最高的10个单词
"""
正则表达式:非字母进行分割
"""
import re
text = open('文件.txt').read()
aList = re.split('\W+', text)
c3 = Counter(aList)
print c3
print c3.most_common(10)