import collections
import re
#匹配单词排名
patt = re.compile("\w+")
counter = collections.Counter(patt.findall(
open('a.txt','r').read()
))
# top 6
for word, times in counter.most_common(6):
print word, times
#匹配词组排名
patt = re.compile("\w+\s\w+")
counter = collections.Counter(patt.findall(
open('a.txt','r').read()
))
# top 6
for word, times in counter.most_common(6):
print word, times
温馨提示:答案为网友推荐,仅供参考