天天看点

Python统计一篇英文文章出现的文字次数的前N名

1,把要统计的文字保存在hamlet.txt中,并和Python程序放在同一目录中

2,以下是Python代码:

import re
from collections import Counter
words = re.findall(r'\w+', open('hamlet.txt').read().lower())
print(Counter(words).most_common(10))