Sorting a 2D numpy array by multiple axes

前端 未结 7 2079
小蘑菇
小蘑菇 2020-11-27 11:23

I have a 2D numpy array of shape (N,2) which is holding N points (x and y coordinates). For example:

array([[3, 2],
       [6, 2],
       [3, 6],
       [3,          


        
7条回答
  •  臣服心动
    2020-11-27 12:00

    I found one way to do it:

    from numpy import array
    a = array([(3,2),(6,2),(3,6),(3,4),(5,3)])
    array(sorted(sorted(a,key=lambda e:e[1]),key=lambda e:e[0]))
    

    It's pretty terrible to have to sort twice (and use the plain python sorted function instead of a faster numpy sort), but it does fit nicely on one line.

提交回复
热议问题