函数式编程reduce
# num=[1,2,3,4,5] # res=0 # for i in num: # res+=num # print(res) # def reduce_test(array): # res=0 # for i in array: # res=num+1 # return res # print(reduce_test(num)) # def multi(x,y): # return x*y # lambda x,y:x*y # def reduce_test(func,array,init=None): # if init is None: # res=array.pop(0)#列表传过来先拿第一个值用pop函数实现 # else: # res=init # for i in array: # res*=func(res,num) # return res # print(reduce_test(lambda x,y:x*y,num,100))#传送的是函数和Num列表 #map原有数据处理一遍,reduce数据压缩一下合并成一个序列最后得到一个结果 #reduce函数需要先导入 from functools import reduce num=[1,2,3,100] print(reduce(lambda x,y:x+y,num,10))
结果116=10+1+2+3+100