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
>>> 

 

相关文章:

  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2021-06-19
  • 2021-08-20
  • 2021-07-22
猜你喜欢
  • 2021-06-07
  • 2021-05-18
  • 2021-10-06
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案