Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这样的结构:
structure = [('a', 1), ('b', 3), ('c', 2)]
我想1+3+2使用sum()内置方法(在一行中)对整数()求和。
1+3+2
sum()
有任何想法吗?
sum(n for _, n in structure)
会工作。
sum(x[1] for x in structure)
应该管用
你可以做
sum(zip(*structure)[1])
使用功能风格,你可以做
reduce(lambda x,y:x+y[1], structure,0)