張泰瑋*_*張泰瑋 -1 python arrays reduce sum
抱歉有一个愚蠢的问题.
我想总结一下清单.但是列表的长度并不总是大于2
因此reduce如果失败则会失败len<2
这是我的代码
score = [('xxx', 1), ('yyy', 2)]
if len(score) >=2:
result = reduce((lambda x,y:x[1]+y[1]), score)
elif len(score)==1:
result = score[0]
else:
result = 0
Run Code Online (Sandbox Code Playgroud)
是否可以在列表长度大于2的情况下以优雅的方式对数组求和?