更新时间:2023-02-10 15:34:28
Python 3:使用functools.reduce
:
Python 3: use functools.reduce
:
>>> from functools import reduce
>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])
720
Python 2:使用reduce
:
Python 2: use reduce
:
>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])
720
要与2和3兼容,请使用pip install six
,然后:
For compatible with 2 and 3 use pip install six
, then:
>>> from six.moves import reduce
>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])
720