import re
from collections import Counter
txt = open('word').read().split() #读取文件中内容,然后切片存储为列表
#new_txt = re.split('\W+',txt)
result = Counter(txt) #统计列表中的词频,存储为字典
print (result)
#print(result.most_common(5)) #打印出现次最多的前5个单词
word = input("Please input a word witch you want to seach:")
search = result.get(word) #从字典中取出用户输入的key对应的value
print("The number of \'%s' you input is " % word,search)
转载于:https://www.cnblogs.com/jacky-zhao/articles/7791679.html