将两个列表一个接一个地组合在一起



我想合并两个列表。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=list1|list2

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数组进行串联。如果你有普通的list而不是numpy数组,那么你可以这样做:

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

但如果您在numpy数组上尝试此操作,它将执行标量加法,而不是串联。