天天看點

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))