If I have
x = [1,2,3,4,5]
y = [6,7,8,9,10]
how can I get a two dimensional array in the form of
combined = [[1,6],[2,7],[3,8],[4,9],[5,10]]
If I have
x = [1,2,3,4,5]
y = [6,7,8,9,10]
how can I get a two dimensional array in the form of
combined = [[1,6],[2,7],[3,8],[4,9],[5,10]]
A few options. Numpy should be your go-to tool for performant array operations. Sticking with your original input values:
np.array([x,y]).T.tolist()
With vanilla python, a list comprehension will do:
[[j,k] for j,k in zip(x,y)]
Or if you don't mind tuples in place of inner lists, you can use the more terse:
list(zip(x,y))
Try numpy if you want:
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]))
output:
[[ 1 6]
[ 2 7]
[ 3 8]
[ 4 9]
[ 5 10]]