reduce函数

from functools import reduce  #从某个模块中导入某个模块

num_1=[2,3,4]

def num_test(func,array,init=None):
    if init is None:  #使用if函数判断用户是否自定义输入了数值
        res=array.pop(0)
    else:
        res=init
    for num in array:
        res=func(res,num)  
    return res

print(num_test(lambda x,y:x*y,num_1,10))
from functools import reduce
num_1=[2,3,4]
print(reduce(lambda x,y:x*y,num_1))

猜你喜欢

转载自www.cnblogs.com/banzui/p/9219839.html
今日推荐