一行至三行命令能做的事

找到在 Hamlet.txt 中出现最多的10个词
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]

参考:
https://docs.python.org/2/library/collections.html

猜你喜欢

转载自blog.csdn.net/m0_37586991/article/details/89739525