Python Note:统计列表,字典中元素的频率
列表
输出列表中出现最多的五个数字
#!/usr/bin/python3
import collections
import random
data = [random.randint(0, 25) for _ in range(0,25)] # 使用列表解析随机生成列表
print(data)
c = collections.Counter(data) # 使用collections.Counter统计
print(c) # 结果
print(c.most_common(5)) # 最多的三个呐
文章词频统计(English)
统计一篇英文文章出现频率最多的10个单词
没有文章随便去哪里复制来就是啦
#!/usr/bin/python3
import collections
import random
import re
txt = open('t.txt').read()
print(txt)
# 使用正则表达式通过非英文字母分割文章 并且统计
c = collections.Counter(re.split('\W+',txt))
print(c.most_common(10))
在无特别说明的情况下,本站文章均遵循 CC BY-NC-SA 4.0
本文链接:https://blog.iknet.top/post/python-data-statistical-frequency.html