Sorting arrays in NumPy by column

后端 未结 13 2449
既然无缘
既然无缘 2020-11-22 03:47

How can I sort an array in NumPy by the nth column?

For example,

a = array([[9, 2, 3],
           [4, 5, 6],
           [7, 0, 5]])

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 04:12

    From the NumPy mailing list, here's another solution:

    >>> a
    array([[1, 2],
           [0, 0],
           [1, 0],
           [0, 2],
           [2, 1],
           [1, 0],
           [1, 0],
           [0, 0],
           [1, 0],
          [2, 2]])
    >>> a[np.lexsort(np.fliplr(a).T)]
    array([[0, 0],
           [0, 0],
           [0, 2],
           [1, 0],
           [1, 0],
           [1, 0],
           [1, 0],
           [1, 2],
           [2, 1],
           [2, 2]])
    

提交回复
热议问题