将两个列表并排组合

Combine two lists one next to another

我想合并两个列表。 list1 是这样的列表或数组:

    list1 = [np.array([14, 17, 17,  8]), np.array([ 7,  7, 19, 16]), np.array([ 9, 18,  2, 19])]

list2是另一个列表,如:

    list2 = [np.array([909]), np.array([909]), np.array([998])]

我想将两个列表连接成 new_listnew_list = [list1, list2],但它没有给我想要的结果。我想生成一个新矩阵,例如:

new_list = 列表 1 |清单 2

14 17 17 8 909

7 7 19 16 990

9 18 2 19 998

这应该有效:

list3 = np.concatenate((list1, list2), axis=1)
# array([[ 14,  17,  17,   8, 909],
#        [  7,   7,  19,  16, 909],
#        [  9,  18,   2,  19, 998]])

请注意,标准 + 不会像普通 python 列表那样对 numpy 数组进行连接。如果你有 vanilla lists 而不是 numpy 数组,那么你可以这样做:

list3 = [a1 + b1 for (a1, b1) in zip(list1, list2)]

但是,如果您在 numpy 数组上尝试这样做,它将进行标量加法而不是串联。