美文网首页
第 0004 题

第 0004 题

作者: 司徒道 | 来源:发表于2017-06-26 16:19 被阅读0次

任一个英文的纯文本文件,统计其中的单词出现的个数
这里的文本文件就用python之禅吧。

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

stop_symbol = ['.', ',', '*', '-', '!', '\n']
word_dict = {}
with open(r'./zen_of_python.txt', 'r') as f:
    for line in f:
        for s in stop_symbol:
            if s in line:
                line = line.replace(s, '')
        words = line.split(' ')
        for w in words:
            if w != '':
                word_dict[w] = word_dict.setdefault(w, 0) + 1

word_count = sorted(word_dict, key=word_dict.get, reverse=True)[:10]
for c in word_count:
    print(c, ': ', word_dict[c])

is : 10
than : 8
better : 8
to : 5
the : 5
idea : 3
one : 3
Although : 3
of : 3
never : 3

相关文章

  • 第 0004 题

    任一个英文的纯文本文件,统计其中的单词出现的个数这里的文本文件就用python之禅吧。 The Zen of Py...

  • 第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。 参考链接:collectionsPython...

  • Python 练习册 0004、0006题 (统计文本)

    第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数第 0006 题:你有一个目录,放了你一个月的日...

  • github/Show me the code (4)

    第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。 模式 描述^ 匹配字符串的开头$ ...

  • python练习册-第0004题

    Description 任一个英文的纯文本文件,统计其中的单词出现的个数。 Analysis 查阅了资料后发现py...

  • PythonShowMeTheCode(0004): 检查单词个

    1. 题目 第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。 2. 效果 将所有单词按照小写处...

  • golang学习---练习4

    第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。 分析:去除非英文字符(空格除外)以空格将文本...

  • 周末作业题

    第1题 第2题 第3题 第4题 第5题 第6题 第7题 第8题 第9题 第10题 第11题 第12题 第13题 第...

  • 10道前端基础题

    第1题 第2题 第3题 第4题 第5题 第6题 第7题 第8题 第9题 第10题

  • 2017年“视界窗教学服务培训”调查问卷统计分析

    第1题 第2题 第3题 第4题 第5题 第6题 第7题 第8题 第9题 第10题

网友评论

      本文标题:第 0004 题

      本文链接:https://www.haomeiwen.com/subject/vqdjcxtx.html