How to concatenate two arrays in numpy python by taking first column from the first array and fisrt column from the second, then second column from first and second from the other, etc..? that is if I have A=[a1 a2 a3]
and B=[b1 b2 b3]
I want the resulting array to be [a1 b1 a2 b2 a3 b3]
如何连接numpy python中的两个数组,从第一个数组获取第一列,从第二个数据获取fisrt列,然后从第一个数据列中获取第二列,从另一个数据中获取第二列等等。那就是如果我有A = [a1 a2 a3]而B = [b1 b2 b3]我希望得到的数组是[a1 b1 a2 b2 a3 b3]
2 个解决方案
#1
1
Few approaches with stacking could be suggested -
很少有人建议使用堆叠方法 -
np.vstack((A,B)).ravel('F')
np.stack((A,B)).ravel('F')
np.ravel([A,B],'F')
Sample run -
样品运行 -
In [291]: A
Out[291]: array([3, 5, 6])
In [292]: B
Out[292]: array([13, 15, 16])
In [293]: np.vstack((A,B)).ravel('F')
Out[293]: array([ 3, 13, 5, 15, 6, 16])
In [294]: np.ravel([A,B],'F')
Out[294]: array([ 3, 13, 5, 15, 6, 16])
#2
0
With numpy.dstack()
and numpy.flatten()
routines:
使用numpy.dstack()和numpy.flatten()例程:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.dstack((a,b)).flatten()
print(result)
The output:
[1 4 2 5 3 6]
#1
1
Few approaches with stacking could be suggested -
很少有人建议使用堆叠方法 -
np.vstack((A,B)).ravel('F')
np.stack((A,B)).ravel('F')
np.ravel([A,B],'F')
Sample run -
样品运行 -
In [291]: A
Out[291]: array([3, 5, 6])
In [292]: B
Out[292]: array([13, 15, 16])
In [293]: np.vstack((A,B)).ravel('F')
Out[293]: array([ 3, 13, 5, 15, 6, 16])
In [294]: np.ravel([A,B],'F')
Out[294]: array([ 3, 13, 5, 15, 6, 16])
#2
0
With numpy.dstack()
and numpy.flatten()
routines:
使用numpy.dstack()和numpy.flatten()例程:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.dstack((a,b)).flatten()
print(result)
The output:
[1 4 2 5 3 6]