我有3个数组[x1,y1,z1],[x2,y2,z2],[x3,y3,z3]我怎么得到[[x1,x2,x3],[y1,y2,y3] ,[z1,z2,z3]]?
我可以做到:
result = np.array([[a1[0],a2[0],a3[0]], [a1[1],a2[1],a3[1]], [a1[2],a2[3],a3[3]]]
有更好的方法吗?
答案 0 :(得分:2)
In [32]: x,y,z = [1,2,3],[11,12,13],[21,21,23]
In [33]: np.array([x,y,z])
Out[33]:
array([[ 1, 2, 3],
[11, 12, 13],
[21, 21, 23]])
转置:
In [34]: np.array([x,y,z]).T
Out[34]:
array([[ 1, 11, 21],
[ 2, 12, 21],
[ 3, 13, 23]])
stack
的行为类似于np.array
,但让我们指定一个轴:
In [35]: np.stack([x,y,z],axis=1)
Out[35]:
array([[ 1, 11, 21],
[ 2, 12, 21],
[ 3, 13, 23]])
对于纯列表解决方案:
In [36]: list(zip(x,y,z))
Out[36]: [(1, 11, 21), (2, 12, 21), (3, 13, 23)]
stacking
的另一种变化:
In [37]: np.column_stack((x,y,z))
Out[37]:
array([[ 1, 11, 21],
[ 2, 12, 21],
[ 3, 13, 23]])