giosans giosans - 3 months ago 8
Python Question

Transform x,y numpy matrixes into a list of (x,y) points

I have

x=np.array([[1,2,3],[3,4,5],[5,7,8]])
y=np.array([[5,2,5],[1,1,1],[2,2,2]])


I'd like to get

xy=[(1,5),(2,2),(3,5),(3,1),(4,1),(5,1),(5,2),(7,2),(8,2)]


What's the best (quick and clean) way to do so? Also, once I have
xy
, how do I relate the indexes of
xy
to the original numpy matrixes
x
,
y
?

Answer

You can use this approach to get xy:

zip(x.flatten(),y.flatten())

#[(1, 5), (2, 2), (3, 5), (3, 1), (4, 1), (5, 1), (5, 2), (7, 2), (8, 2)]
Comments