1.encoding='utf-8’用于解决python3里,文件中有中文字符编码的bug
2.将lambda函数作为参数传递给其他函数
sorted函数:此时lambda函数用于指定对列表中所有元素进行排序的准则。
import string
with open('Walden.txt','r',encoding='utf-8') as text:
#去掉文字首位的标点符号,并把首字母大写转换成小写
= [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]
#将列表用set函数转换成集合
words_index = set(words)
#创建一单词为key,频率为值的字典
counts_dict = {index:words.count(index) for index in words_index}
#打印整理后的参数,其中利用lambda表达式,以字典中的值为排序的参数
for word in sorted(counts_dict,key=lambda x: counts_dict[x],reverse=True):
print('{} -- {} times'.format(word,counts_dict[word]))


561

被折叠的 条评论
为什么被折叠?



