python reduce初始化程序错误

时间:2015-03-10 22:03:23

标签: python functional-programming reduce

我正在尝试使用python的reduce以类似于racket的foldl的方式,但是当我运行以下代码时:

functools.reduce(lambda x, y: x.append(y), [1,2,3], [])
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<input>", line 1, in <lambda>
AttributeError: 'NoneType' object has no attribute 'append'

你能帮忙解释一下这个错误并提出修正建议吗?

1 个答案:

答案 0 :(得分:4)

那是因为append()没有返回任何内容。

你可以这样做:

functools.reduce(lambda x, y: x + [y], [1,2,3], [])