2018-08-31第4个小程序:统计文章中单词出现次数

import string
path = "C://Users/seewo.chen/Downloads/Walden.txt"
newpath = "C://Users/seewo.chen/Downloads/Walden_new.txt"
file = open(path, encoding='gb18030', errors='ignore')
file_new = open(newpath, 'w')
file_new.write(file.read())
file.close()
file_new.close()
with open(newpath, 'r') as text:
    words = [raw_word.strip(string.punctuation).lower()
             for raw_word in text.read().split()]
    words_index = set(words)  
    counts_dict = {index: words.count(index) for index in words_index}
    for word in sorted(counts_dict, key=lambda x: counts_dict[x], reverse=True):
        print('{}--{} times'.format(word, counts_dict[word]))
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在我们的一生中有的时候太累了,身体的疲劳,心灵的痛苦,而我们却不知所然,累了,只有自己知道,苦了,也只有自己明白,...
    月古君阅读 1,355评论 0 0
  • 4+5=9,这是我们在一起的第45天,也是我们的第一个情人节,第一个等式或许就是一种暗示吧,也许上天注定我们要99...
    好奇的猫OMG阅读 3,352评论 2 0
  • 在架构设计中,逻辑架构又是重要的架构设计之一。因为逻辑架构也是面众广泛的设计,需要面对客户、分析人员、设计人员、开...
    小七赛文阅读 5,702评论 0 2
  • 一、安装docker 1、卸载旧版本 2、安装依赖包 3、添加yum软件源 4、安装docker-ce 安装完设置...
    nikori1990阅读 4,104评论 0 1

友情链接更多精彩内容