【发布时间】:2017-11-01 21:29:20
【问题描述】:
我有
>>> reduce( (lambda x,y: x + y), [1,2,3])
6
和
>>> reduce( (lambda x,y: x + y), ['cat','dog','hat'])
'catdoghat'
然后,我想得到列表中所有字符串元素的总和:
>>> reduce( (lambda x,y: len(x) + len(y)), ['cat','dog','hat'])
它会产生错误。
我做错了什么?
【问题讨论】:
-
reduce(lambda x,y: x + y, map(len, ['cat','dog','hat']))