1 回答
![?](http://img1.sycdn.imooc.com/54584ee0000179f302200220-100-100.jpg)
TA贡献1911条经验 获得超7个赞
使用 numpy 广播并可扩展到任意数量的数组:
r1,c1 = A.shape
r2,c2 = B.shape
arrOut = np.zeros((r1,r2,c1+c2), dtype=A.dtype)
arrOut[:,:,:c1] = A[:,None,:]
arrOut[:,:,c1:] = B
arrOut.reshape(-1,c1+c2)
输出:
[[1 4 2 3]
[1 4 1 3]
[3 5 2 3]
[3 5 1 3]
[1 2 2 3]
[1 2 1 3]]
对于 3 数组的情况(这里我使用了 (A,B,A)):
r1,c1 = A.shape
r2,c2 = B.shape
r3,c3 = A.shape
arrOut = np.zeros((r1,r2,r3,c1+c2+c3), dtype=A.dtype)
arrOut[:,:,:,:c1] = A[:,None,None,:]
arrOut[:,:,:,c1:c1+c2] = B[:,None,:]
arrOut[:,:,:,c1+c2:] = A
arrOut.reshape(-1,c1+c2+c3)
输出:
[[1 4 2 3 1 4]
[1 4 2 3 3 5]
[1 4 2 3 1 2]
[1 4 1 3 1 4]
[1 4 1 3 3 5]
[1 4 1 3 1 2]
[3 5 2 3 1 4]
[3 5 2 3 3 5]
[3 5 2 3 1 2]
[3 5 1 3 1 4]
[3 5 1 3 3 5]
[3 5 1 3 1 2]
[1 2 2 3 1 4]
[1 2 2 3 3 5]
[1 2 2 3 1 2]
[1 2 1 3 1 4]
[1 2 1 3 3 5]
[1 2 1 3 1 2]]
您甚至可以为 N 数组情况创建一个 for 循环。
添加回答
举报