python 使用Counter统计列表中元素的频度

来源:互联网 发布:单片机驱动大功率led 编辑:程序博客网 时间:2024/06/05 09:49
from collections import Counterimport re# 统计列表中次数出现最高的单词的频次a = [12, 5, 6, 4, 6, 5, 5, 7]print(Counter(a).most_common())# 拥挤英文文章中单词的频度with open("LICENSE") as f:    contend = f.read()# 将文本转换为单词列表contend = re.split("\W+", contend)  # \W匹配任何非单词字符 \w匹配包括下划线的任何单词字符print(Counter(contend).most_common(10))
阅读全文
0 0
原创粉丝点击