文件方式实现完整的英文词频统计实例

>>> fo.open('coffee.txt','r')
coffee=fo.read()
coffee=coffee.lower()
for i in ',.':
coffee=coffee.replace(i,' ')
words=coffee.split(' ')
exp={'','the','a','was','of','and','that','he','in'}
dict={}
keys=set(words)-exp
for i in keys:
dict[i]=words.count(i)
count=list(dict.items())
count.sort(key=lambda x:x[1],reverse=True)
for i in range(20):
print(count[i])

posted @ 2017-09-26 09:55  017廖佳辉  阅读(75)  评论(0编辑  收藏  举报