from collections import Counter
import 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))
python 使用Counter统计列表中元素的频度
最新推荐文章于 2023-06-02 11:41:43 发布