import string #需要用到这个包来去掉符号
with open ('E:/learning/python/script/Walden.txt',encoding='utf-8','r') as text: #刚下载的txt有时候打不开,不要用到encoding来打开
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:count_dict[x],reverse=True):
print('{}:{} times' .format(word,counts_dict[word]))
网友评论