reduce python 的用法

Posted 左丿仔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了reduce python 的用法相关的知识,希望对你有一定的参考价值。

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 的用法的主要内容,如果未能解决你的问题,请参考以下文章

reduce python 的用法

Python的functools.reduce用法

Python内建函数reduce()用法

python reduce的用法

python reduce的用法

python map(),reduce()函数的用法