Python词频统计的两种方法详解

这篇文章主要为大家介绍了Python词频统计,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

统计文件里每个单词的个数

思路:

分别统计文档中的单词,与出现的次数

用两个列表将其保存起来,最后再用zip()函数连接输出**

想法成立开始实践

方法一:

 # 导入文件 with open("passage.txt", 'r') as file: dates = file.readlines() # 处理 words = [] for i in dates: words += i.replace("\n", "").split(" ")  # 用空字符来代替换行 words +是为了不被覆盖无+将只有最后一条数据 # print(i.replace("\n","").split(" ")) setWords = list(set(words))  # 集合自动去重 num = []  # 统计一个单词出现的次数 for k in setWords: count = 0 for j in words: if k == j: count = count + 1 num.append(count) print(num) print(setWords) # 输出 for x, y in zip(setWords, num):  # 将两个列表用zip结合 print(x + ":" + str(y))、 

效果图:

在这里插入图片描述

方法二:

此方法用来字典,较前一个相对简洁一点

 # 导入 with open("passage.txt", 'r') as file: dates = file.readlines() # 处理 words = [] for i in dates: words += i.replace("\n", "").split(" ") # print(i.replace("\n","").split(" ")) # setWords=list(set(words))  #可以不用这个 print(words) print("-" * 40) # print(setWords) diccount = dict() for i in words: if (i not in diccount): diccount[i] = 1  # 第一遍字典为空 赋值相当于 i=1,i为words里的单词 # print(diccount) else: diccount[i] = diccount[i] + 1  # 等不在里面的全部遍历一遍赋值就都在里面了,我们再来记数 print(diccount) 

效果图:

在这里插入图片描述

统计的文档

在这里插入图片描述

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注0133技术站的更多内容!

以上就是Python词频统计的两种方法详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python