如何对多个列表中多个列表的相同索引的值求和?

How to sum the values from same index from multiple lists within multiple lists?

我有一个包含多个列表的列表,同时这些列表包含多个列表。为简单起见,假设我有:

x = [
    [[1, 0], [0, 0], [0 , 0], [1, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
    ]

让我们考虑以下变量:

y = [[0, 0], [0, 2], [0 , 0], [0, 0], [0, 1], [0, 0], [0, 1], [0, 0], [0, 1]]

有没有更pythonic的获取方式:

res = [[1, 0], [0, 2], [0 , 0], [1, 0], [0, 1], [0, 0], [0, 1], [0, 0], [0, 1]]

除了:

for i in range(len(y)):
    res.append([y[i][0] + x[0][i][0], y[i][1] + x[0][i][1]])

您可以使用列表理解并在 1 行中做 t:

x_list = x[0]  # Not sure why x is inside a list but I'm going with it
result = [[sum(subitmes) for subitems in zip(*items)] for items in zip(x_list, y)]