在Python中用两个一维数组创建二维python数组

时间:2018-03-18 21:29:14

标签: python arrays

如果我有 x = [1,2,3,4,5]
y = [6,7,8,9,10]

如何以
的形式获得二维数组     combined = [[1,6],[2,7],[3,8],[4,9],[5,10]]

2 个答案:

答案 0 :(得分:-1)

使用列表理解:

[[j,k] for j,k in zip(x,y)]

答案 1 :(得分:-2)

如果你想要尝试numpy:

import numpy as np

x = [1,2,3,4,5]
y = [6,7,8,9,10]

print(np.reshape(np.dstack((x,y)),[-1,2]))

输出:

[[ 1  6]
 [ 2  7]
 [ 3  8]
 [ 4  9]
 [ 5 10]]