【python】从一个文件中,查找出现次数最多的5个词

#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]

展开阅读全文

没有更多推荐了,返回首页