【python】从一个文件中,查找出现次数最多的5个词
#coding = utf-8def find_word(filename):resultdict={}with open(filename)as fp:for i in fp:print iwordlist=i.split()for j in wordlist:if not resultdict.has_key(j):resultd...
·
#coding = utf-8
def find_word(filename):
resultdict={}
with open(filename)as fp:
for i in fp:
print i
wordlist=i.split()
for j in wordlist:
if not resultdict.has_key(j):
resultdict[j]=1
else:
resultdict[j]+=1
print resultdict
sort_resultdict=sorted(resultdict.items(),key=lambda x:x[0],reverse=True)
return sort_resultdict
print find_word("d:\\test.txt")[:6]
更多推荐
已为社区贡献1条内容
所有评论(0)