如何合并行数相同但列数不同的两个numpy数组

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

给出这两个numpy数组:

# Two 2-dim arrays with same row number but differnt column
a1 = np.array([[9,9,9,9], [9,9,9,9],[9,9,9,9]], dtype=np.int64)
a2 = np.array([[3],[3],[3]], dtype=np.int64)

如何将它们合并到两个数组中,将创建第三个数组,如下所示:

# Third array with same row numbers as above two arrays but its column number is the sum of column numbers of the above two arrays
[[9,9,9,9,3],
 [9,9,9,9,3],
 [9,9,9,9,3]]

简单来说,如何将一列连接到二维数组?

python
1个回答
0
投票
import numpy as np

a1 = np.array([[9,9,9,9], [9,9,9,9],[9,9,9,9]])
a2 = np.array([[3],[3],[3]])
print(np.concatenate((a1, a2), axis=1))

https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html

Join a sequence of arrays along an existing axis.

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