我有两个numpy数组x1和x2。使用Python 3.4.3。
x1 = np.array([2,4,4])
x2 = np.array([3,5,3])
我希望创建一个类似于这样的numpy数组:
[[2,3],[4,5],[4,3]]
我该如何处理这个问题?
numpy.column_stack
函数:In [40]: x1 = np.array([2,4,4])
In [41]: x2 = np.array([3,5,3])
In [42]: np.column_stack((x1, x2))
Out[42]:
array([[2, 3],
[4, 5],
[4, 3]])
没错,这听起来像是zip函数:
import numpy as np
x1 = np.array([2,4,4])
x2 = np.array([3,5,3])
print zip(x1, x2) # or [list(i) for i in zip(x1, x2)]
zip
函数将这2个数组压缩起来:x3 = list(zip(x1,x2))
输出:
[(2, 3), (4, 5), (4, 3)]
以上代码创建了一个元组列表
。如果你想要一个列表
的列表
,你可以使用列表推导式
:
x3 = [list(i) for i in list(zip(x1,x2))]
输出:
[[2, 3], [4, 5], [4, 3]]
[[2,3],[4,5],[4,3]]
。我知道他称之为 numpy 数组... - Joe T. Boka