鱼C论坛

 找回密码
 立即注册
查看: 1526|回复: 3

[已解决]有个别语句不太懂,能否解释一下大伙儿?

[复制链接]
发表于 2017-7-25 19:51:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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):
这几句话如何解释啊?谢谢各位!
最佳答案
2017-7-25 21:20:16
唐长老爱女人 发表于 2017-7-25 20:29
可以运行啊,只是你没有那个路径的文本而已

2017-07-25_211716.png

sorted 说明:http://www.cnblogs.com/kellyseeme/p/5525052.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-25 20:28:39 | 显示全部楼层
不好意思,你代码运行不了。所以也没法解释
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-25 20:29:44 | 显示全部楼层
ba21 发表于 2017-7-25 20:28
不好意思,你代码运行不了。所以也没法解释

可以运行啊,只是你没有那个路径的文本而已
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-25 21:20:16 | 显示全部楼层    本楼为最佳答案   
唐长老爱女人 发表于 2017-7-25 20:29
可以运行啊,只是你没有那个路径的文本而已

2017-07-25_211716.png

sorted 说明:http://www.cnblogs.com/kellyseeme/p/5525052.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-24 20:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表