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

python中的reduce

时间:2018-12-08 14:43:55      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:init   port   lam   col   pytho   none   span   bsp   style   

from functools import reduce

num_1 = [1, 2, 3, 4, 5]

#加法
def reduce_test(array):
    res = 0
    for num in num_1:
        res += num
    return res

#乘法

def reduce_test(func, array, init=None):
    if init is None:
        res = array.pop(0)
    else:
        res = init
    for num in num_1:
        res *= func(res, num)
    return res

print(reduce_test(lambda x, y: x*y, num_1)

# reduce函数
 res = reduce(lambda x, y: x+y, num_1, 1)  
 print(res)   

 

python中的reduce

标签:init   port   lam   col   pytho   none   span   bsp   style   

原文地址:https://www.cnblogs.com/majianyu/p/10087529.html

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