词频统计

# with fo=open('Opium.txt','r') as f:
fo = open(file='Opium.txt', mode='r')
s=fo.read()
fo.close()


exclude_list=["'"]
for exclude in exclude_list:
    s=s.replace(exclude," ")

words=set(s.lower().split())
words=words-set({"a","the","am","to","i"})

dict={}

for word in words:
    dict[word]=s.lower().count(word)

dict=sorted(dict.items(),key=lambda x:x[1],reverse=True)
print(dict[:10])

posted @ 2018-03-26 11:42  126刘畅  阅读(95)  评论(0编辑  收藏  举报