diff --git a/homework_01_python/README.md b/homework_01_python/README.md index ff6e0f10c5c4d3c0a599f35bb4e0bfdca869001f..026473ff83fa9116c72089e43324d5df07e6c23d 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`的情况下程序会不会有问题?