Python查找一个文章里出现次数最多的10的单词

来源:互联网 发布:淘宝云客服招聘 编辑:程序博客网 时间:2024/05/22 15:10

用字典格式实现:

f = open('novel.txt')word_freq = {}for line in f:words = line.strip().split()for word in words:if word in word_freq:word_freq[word] += 1else:word_freq[word] = 1freq_word = []for word,freq in word_freq.items():freq_word.append((word,freq))freq_word.sort(reverse = True)for freq,word in freq_word[:10]print wordf.close()



原创粉丝点击