functional programming 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 )#The list is passed over and first take the first value and implement it with the pop function
#     else:
#         res=init
#     for i in array:
# res *= func(res,num)
#     return res
# print(reduce_test(lambda x,y:x *y,num, 100 ))#The function and Num list are passed
#map The original data is processed once, the reduce data is compressed and merged into a sequence and finally a result is obtained
#reduce function needs to be imported first
 from functools import reduce
num =[ 1 , 2 , 3 , 100 ]
print(reduce(lambda x,y:x+y,num,10))

Result 116=10+1+2+3+100

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324976141&siteId=291194637