统计一篇英文文章内每个单词出现频率,并返回出现频率最高的前10个单词及其出现次数
from collections import Counter import re with open('a.txt', 'r', encoding='utf-8') as f: txt = f.read() c = Counter(re.split('\W+',txt)) #取出每个单词出现的个数 print(c) ret = c.most_common(10) #取出频率最高的前10个 print(ret)
from collections import Counter import re with open('a.txt', 'r', encoding='utf-8') as f: txt = f.read() c = Counter(re.split('\W+',txt)) #取出每个单词出现的个数 print(c) ret = c.most_common(10) #取出频率最高的前10个 print(ret)