|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import string
path = 'C:\\Users\\lenovo\\Desktop\\Walden.txt'
with open(path,'r',encoding = 'utf-8') 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}
for word in sorted(counts_dict,key = lambda x:counts_dict[x],reverse = True):
if counts_dict[word] > 1:
print('{} -- {} times '.format(word,counts_dict[word]))
else:
print('{} -- {} time'.format(word, counts_dict[word]))
其中:
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:counts_dict[x],reverse = True):
这几句话如何解释啊?谢谢各位!
sorted 说明:http://www.cnblogs.com/kellyseeme/p/5525052.html
|
|