瓦爾登湖各單詞出現頻次,並按排次由高到低排序

genuineZ2發表於2020-11-12

瓦爾登湖各單詞出現頻次,並按排次由高到低排序

如圖輸入

import string
with open(path,'r',encoding='utf-8') as text:
    raw_words = text.read().split()
    words_list = [word.strip(string.punctuation).lower() for word in raw_words]
    words_set = set(words_list)
    words_dict = {keyword:words_list.count(keyword) for keyword in words_set}
for word in sorted(words_dict.items(), key = lambda word: word[1], reverse = True):
    print('{}--{} times'.format(word[0],word[1]))

可得到結果
在這裡插入圖片描述

相關文章