数组中三个列表的元素明智相加

问题描述 投票:0回答:1

假设我有这个数组,

arr = [[7, 6], [4, 2, 7],[4,4,6,2]]

我想要一个像这样的新列表,

list = [15,10,13,2]

我怎样才能得到它,我正在使用以下方法,但它不起作用,

list = np.sum(arr,axis=0)

请问有人可以帮忙吗?

arrays numpy addition
1个回答
0
投票

您可以使用

itertools.zip_longest

from itertools import zip_longest

arr = [[7, 6], [4, 2, 7],[4,4,6,2]]

out = list(map(sum, zip_longest(*arr, fillvalue=0)))

输出:

[15, 12, 13, 2]

© www.soinside.com 2019 - 2024. All rights reserved.