在numpy N维数组中水平堆叠相同索引

时间:2019-01-11 16:25:23

标签: python arrays numpy

是否有一种更pythonic /更智能/更有效的方法来创建新的numpy数组,其中每个元素都是N个元素在同一位置的水平堆叠?

示例输入:

a = [
 [ 0.29425778  0.69311652]
 [ 1.64811132 -0.21107166]
] 
b = [
  [ 0.15356808 -1.00027092]
  [[-1.578063    0.29741589]
]

结果:

[
  array([0.29425778, 0.15356808]), 
  array([ 0.69311652, -1.00027092]), 
  array([ 1.64811132, -1.578063  ]), 
  array([-0.21107166,  0.29741589])
]

现在,我只使用嵌套的for循环,显然可以。

欢呼声, FB

1 个答案:

答案 0 :(得分:1)

使用numpy.dstackreshape

import numpy as np

a = np.array([[0.29425778, 0.69311652], [1.64811132, -0.21107166]])
b = np.array([[0.15356808, -1.00027092],[-1.578063, 0.29741589]])

result = np.dstack((a, b)).reshape(-1, 2)
print(result)

输出

[[ 0.29425778  0.15356808]
 [ 0.69311652 -1.00027092]
 [ 1.64811132 -1.578063  ]
 [-0.21107166  0.29741589]]
相关问题