Python3 统计某字符串重复次数:

from functools import reduce

sentences = ["The Deep Learning textbook is a resource intended to help students and practitioners enter the field of machine learning in general and deep learning in particular. "]

word_count =reduce(lambda a,x:a+x.count("learning"),sentences,0)

print(word_count)

输出结果为:

2

当然,如果只是单纯实现功能可以使用以下方式:

from functools import reduce

sentences = ["The Deep Learning textbook is a resource intended to help students and practitioners enter the field of machine learning in general and deep learning in particular. "]

print(sentences[0].count("learning"))

763***548@qq2年前 (2018-06-14)

更多推荐

pythonlambda菜鸟教程-Python reduce() 函数