ll=[21,2,[3,54],[23,3,5],5,[2,5,76,23],56,[6,5,43,56],3,[56,76],89,7]
计算所有数字的和
如果只有一层嵌套可以这么写
total=0
for ele in ll:
if (isinstance(ele,list)):
total += sum(ele)
else:
total += ele
def cal(ele):
total = 0
if (isinstance(ele,list)):
for e in ele:
total += cal(e)
else:
total += ele
return total
ll = [21,2,[3,54],[23,3,5],5,[2,5,76,23],56,[6,5,43,56],3,[56,76],89,7]
print(cal(ll))