码迷,mamicode.com
首页 > 编程语言 > 详细

reduce python 的用法

时间:2017-12-13 23:48:43      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:red   seq   imp   color   and   ini   value   init   post   

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

标签:red   seq   imp   color   and   ini   value   init   post   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!