From 2c86da5833a5ca37e3a9955bad79ab8d42d782ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=82=E5=8D=A0=E5=85=88?= <1969479820@qq.com> Date: Tue, 8 Dec 2020 14:59:14 +0800 Subject: [PATCH] update homework_01_python/README.md. --- homework_01_python/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/homework_01_python/README.md b/homework_01_python/README.md index ff6e0f1..026473f 100644 --- a/homework_01_python/README.md +++ b/homework_01_python/README.md @@ -9,6 +9,20 @@ ``` One is always on a strange road, watching strange scenery and listening to strange music. Then one day, you will find that the things you try hard to forget are already gone. ``` +程序如下: +import string + +path = r'C:\Users\桂尔哈提\PycharmProjects\pythonProject1\en.txt' +with open(path, '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} # 通过index写入字典 + + for word in sorted(counts_dict, key=lambda x: counts_dict[x], reverse=True): # 通过字典进行字数统计 + print('{}-{} times'.format(word, counts_dict[word])) + + + **深入思考:** * 写完程序之后反思一下,如果单词之间是两个空格,或者是`\t`的情况下程序会不会有问题? -- Gitee