python练手小题(三)

统计个数

# 首先随机生成20个整数
data3 = [randint(0,10) for i in range(20)]

# F1
from collections import Counter
c2 = Counter(data3)
print(c2)
print(c2.most_common(3))  # 找到出现频度最高的3个


# F2 对文件里面的单词进行统计
import re
cc=open('xx.txt').read()
c3 = Counter(re.split('\w+',cc))
c3.most_common()
posted @ 2018-08-05 21:29  此时相望不相闻  阅读(60)  评论(0编辑  收藏  举报