0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

numpyで第一要素でSortし,同一値の場合は第二要素でSortする方法

Posted at

np.lexsort によってargsortができるようです.
ただし,一番最初にSortをするKeyが引数の最後に来ることに注意が必要です.

import numpy as np


a = np.array([ 
    [2, 1, 2, 3, 1, 4, 3],
    [4, 1, 3, 2, 3, 3, 5],
]).T

order = np.lexsort((a[:, 1], a[:, 0]))

>>> a[order]
array([[1, 1],
       [1, 3],
       [2, 3],
       [2, 4],
       [3, 2],
       [3, 5],
       [4, 3]])
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?