I have two lists like these:
x = np.array([1,2,3,4,5])
y_all = [[2,3,5,4,6],
[3,2,4,3,5],
[4,5,4,3,4]]
And I need the output to be like this:
[(1, 2), (2, 3), (3, 5), (4, 4), (5, 6), (1, 3), (2, 2), (4, 3), (5, 5), (1, 4), (2, 5), (3, 4), (4, 3), (5, 4),]
How can I do this more efficiently so that if I have a larger size of x and y_all I can do that?
This is what I have tried so far:
import numpy as np
y=np.array(y_all).T
y_all_fin = []
for i in range(len(y[0])):
inter = np.vstack((x,y_all[i]))
y_all_fin.append(inter)
points = np.hstack((y_all_fin[0],y_all_fin[1],y_all_fin[2])).T
N = []
for i in range(len(points[:,0])):
new = tuple(points[i])
N.append(new)
CodePudding user response:
You can do it with this simple 1-liner:
[a for y in y_all for a in zip(x,y)]
Note that there is little point using numpy if your arrays contain object (non numerical) types like tuples...