python 递归求和
例子:
1 mylist = [1,2,3,4,5] 2 def func(var): 3 if var == []: 4 print('列表为空') 5 return 0 6 return var[0] + func(var[1:]) 7 #递归求和 8 # 1 + func([2,3,4,5]) 9 # 2 + func([3,4,5,]) 10 # 3 + func([4,5]) 11 # 4 + func([5]) 12 # 5 + [] 13 # 0 14 mysum = func(mylist) 15 print(mysum)
输出结果:列表为空
15