0

I am currently working on coordinates and somehow, I need to sort the x-coordinates in ascending order having the same y-coordinates without touching the y-coordinates.

Here's the sample input:

x = [213,212,213,214,215,216,215,216,217,216,218,217,219,220,219]
y = [352,333,332,330,328,327,327,326,325,325,324,324,323,322,322]
xy = np.array([x,y])

Here's the desired output:

x = [213,212,213,214,215,215,216,216,216,217,217,218,219,219,220]
y = [352,333,332,330,328,327,327,326,325,325,324,324,323,322,322]

Note: I have been searching for this one but I can't seem to find a direct answer. Thank you.

4
  • sort the x list first? Commented Jun 2, 2022 at 16:50
  • @Copperfield I'm not sure if sorting the x list first is the same idea because it might change the order of the y list. I only need to sort the x having the same y values without touching the y. Commented Jun 2, 2022 at 16:56
  • in your example the x list is independent from the y list, whatever you do the x list have no effect on the y list Commented Jun 2, 2022 at 17:01
  • @Copperfield sorry for the wrong example, i somehow updated it Commented Jun 2, 2022 at 17:35

1 Answer 1

1

you can first make a list of pair and order that by the first coordinate and them make it into a numpy array

>>> x = [213,212,213,214,215,216,215,216,217,216,218,217,219,220,219]
>>> y = [352,333,332,330,328,327,327,326,325,325,324,324,323,322,322]
>>> xy=sorted(zip(x,y),key=lambda pair:pair[0])
>>> xy
[(212, 333), (213, 352), (213, 332), (214, 330), (215, 328), (215, 327), (216, 327), (216, 326), (216, 325), (217, 325), (217, 324), (218, 324), (219, 323), (219, 322), (220, 322)]
>>> nxy = np.array(xy).T
>>> nxy
array([[212, 213, 213, 214, 215, 215, 216, 216, 216, 217, 217, 218, 219,
        219, 220],
       [333, 352, 332, 330, 328, 327, 327, 326, 325, 325, 324, 324, 323,
        322, 322]])
>>> nxy
array([[212, 213, 213, 214, 215, 215, 216, 216, 216, 217, 217, 218, 219,
        219, 220],
       [333, 352, 332, 330, 328, 327, 327, 326, 325, 325, 324, 324, 323,
        322, 322]])
>>> 

after some good old trial and error find a way to sort them in numpy

>>> x = [0, 500, 100, 300, 200, 800, 400, 600, 700, 900]
>>> y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> xy= np.array([x,y])
>>> xy
array([[  0, 500, 100, 300, 200, 800, 400, 600, 700, 900],
       [  0,   1,   2,   3,   4,   5,   6,   7,   8,   9]])
>>> np.argsort(xy[0]) #get an array with the index that correspond to the elements in a sorted fashion 
array([0, 2, 4, 3, 6, 1, 7, 8, 5, 9], dtype=int64)
>>> xy[:,np.argsort(xy[0])] #ask for the elements in the aforementioned order, while preserving their pairing
array([[  0, 100, 200, 300, 400, 500, 600, 700, 800, 900],
       [  0,   2,   4,   3,   6,   1,   7,   8,   5,   9]])
>>> 
Sign up to request clarification or add additional context in comments.

13 Comments

Interesting. How does sorting xy[0] sort both rows correspondingly? Where do I look in the documentation for that?
I suppose it should be somewhere in the numpy documentation, I was testing to see if it worked or not
Oh wait, I thought I had seen a difference in the second row before and after but I don't see it anymore, so I can't tell whether the second row was touched or not.
in the second example you can see that the second row wasn't affected by sorting only the first
Maybe. The whole question is really unclear and weird. Among other issues, they say "having the same y-coordinates without touching the y-coordinates", which sounds like saying the same thing twice. So presumably the two things don't mean the same? But then what do they mean? It's a mystery...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.