标签:set range items for 词频统计 read 方式 txt ict
>>> 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])
标签:set range items for 词频统计 read 方式 txt ict
原文地址:http://www.cnblogs.com/coffee2/p/7595191.html