小编d78*_*89w的帖子

在python中使用函数sum()和列表推导

我有一个需要列表理解的问题并且必须使用sum()函数,它可能不是最好的方法,但这就是要求的.请阅读以下问题:

问题: 编写一个使用列表推导的函数word_count(字符串,单词)和sum()函数来计算单词在字符串中出现的次数.将此应用于狄更斯弦.提示:sum()函数可用于添加列表的元素.例如,sum([1,2,3])将返回6.某些单词是否有问题?哪些是为什么?尝试使用条带字符串方法(我们稍后在讨论正则表达式时再次访问它).

使用字符串:

dickens = """
It was the best of times, it was the worst of times, 
it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, 
it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, 
it was the spring of hope, it was the winter of despair, we had everything before us, we had 
nothing before …
Run Code Online (Sandbox Code Playgroud)

python list-comprehension sum function list

1
推荐指数
1
解决办法
1523
查看次数

标签 统计

function ×1

list ×1

list-comprehension ×1

python ×1

sum ×1