首页 > 编程语言 > 详细

reduce python 的用法

时间:2017-12-13 22:35:28      阅读:242      评论:0      收藏:0      [点我收藏+]

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 的用法

原文:http://www.cnblogs.com/sxwen/p/8034323.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!