我正在尝试对列表中每个元素的第一项求和。
我尝试打印变量sum来查看发生了什么,并且似乎给出了正确元素的列表。
这是我的代码:
def define_elements(points):
for sublist in points:
sum = 0
sum += sublist[0]
return sum
print(define_elements(datapoints) ```
When I run the following code it prints -1. I'm looking for an answer of 10.
答案 0 :(得分:2)
ENTRYPOINT
O / P:
datapoints = [(1, 1), (3, 3), (5, 5), (-1, -1)]
total = sum(i[0] for i in datapoints)
print(total)
答案 1 :(得分:0)
def define_elements(points):
return sum(map(lambda x:x[0], points))