1. 程式人生 > >reduce python 的用法

reduce python 的用法

placed pos app default tool value nbsp gpo too

1.查看reduce 的用法

在python 命令查看
 import functools
 help(functools)

 help(functools.reduce)

或者

 from functools import reduce
 help(reduce)


Help on built-in function reduce in module _functools:

reduce(...)
    reduce(function, sequence[, initial]) -> value
    
    Apply a function of two arguments cumulatively to the items of a sequence,
    
from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence
is empty. >>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 15 >>> reduce(lambda x, y: x*y, [1, 2, 3, 4, 5]) 120 >>>

reduce python 的用法