你的问题本身有点不清楚。无论如何,我只是假设——
>>> a = []
>>> a.append((1,2,3))
>>> a.append((7,2,4))
>>> a
[(1, 2, 3), (7, 2, 4)] # list of tuples
我假设您可能对获取列表中所有元素的总和感兴趣。如果这是问题,那么可以分两步解决
1)第一步应该是展平列表。
2)然后添加列表的所有元素。
>>> new_list = [y for x in a for y in x] # List comprehension used to flatten the list
[1, 2, 3, 7, 2, 4]
>>> sum(new_list)
19
一个班轮
>>> sum([y for x in a for y in x])
19
另一个假设,如果你的问题是在列表中逐项减去元组的每个元素,那么使用这个:
>>> [tuple(map(lambda y: abs(item - y), x)) for x in a]
[(0, 1, 2), (6, 1, 3)] # map function always returns a list so i have used tuple function to convert it into tuple.
如果问题是其他问题,请详细说明。
PS:Python 列表理解比其他任何东西都要好和高效。