利用python处理文档中各字段出现的次数并排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用python处理文档中各字段出现的次数并排序相关的知识,希望对你有一定的参考价值。

1 import string
2 path = waldnn
3 with open(path,r) as text:
4     words = [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]
5     words_index = set(words)
6     counts_dict = {index:words.count(index) for index in words_index}
7 
8 for word in sorted(counts_dict,key=lambda x: counts_dict[x],reverse=True):
9     print({} -- {} times.format(word,counts_dict[word]))

 

以上是关于利用python处理文档中各字段出现的次数并排序的主要内容,如果未能解决你的问题,请参考以下文章