python 3 functools.reduce 如果没有给出文字列表则给出 0
python 3 functools.reduce gives 0 if not literal list given
我正在尝试减少 python 并在期望其他值时得到 0:
In [1]: from functools import reduce
In [2]: reduce( (lambda x, y: x * y), [1, 2, 3, 4] )
Out[2]: 24
In [3]: def multiply(x, y): return x * y
In [4]: reduce( multiply, [1, 2, 3, 4] )
Out[4]: 24
In [5]: reduce( multiply, range(5) )
Out[5]: 0
In [6]: reduce( multiply, list(range(5)) )
Out[6]: 0
[...]
In [11]: L = list(range(5))
In [12]: L
Out[12]: [0, 1, 2, 3, 4]
In [13]: reduce(multiply, L)
Out[13]: 0
为什么我没有输入文字列表时得到 0?如何减少任意列表?我错过了什么?
Python 的 range
以 0
开头,而不是 1
。 0
次任何事情都会导致 0
你总是得到...
我正在尝试减少 python 并在期望其他值时得到 0:
In [1]: from functools import reduce
In [2]: reduce( (lambda x, y: x * y), [1, 2, 3, 4] )
Out[2]: 24
In [3]: def multiply(x, y): return x * y
In [4]: reduce( multiply, [1, 2, 3, 4] )
Out[4]: 24
In [5]: reduce( multiply, range(5) )
Out[5]: 0
In [6]: reduce( multiply, list(range(5)) )
Out[6]: 0
[...]
In [11]: L = list(range(5))
In [12]: L
Out[12]: [0, 1, 2, 3, 4]
In [13]: reduce(multiply, L)
Out[13]: 0
为什么我没有输入文字列表时得到 0?如何减少任意列表?我错过了什么?
Python 的 range
以 0
开头,而不是 1
。 0
次任何事情都会导致 0
你总是得到...