54
27

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 5 years have passed since last update.

Pythonでリストのn番目に大きい・小さい値のインデックスの取得

Last updated at Posted at 2016-08-10

リスト操作をするときとかにn番目にデカい値までのindexが欲しいということが度々ある。
今までは適当にこねくり回して書いていたがnumpyを用いるとスマートにかけると知ったので自分用にメモ

a=[1,5,4,3,2]

というリストがあったとき

>>> A=np.array(a)
>>> A.argsort()
array([0, 4, 3, 2, 1])

とndarrayに対してargsort()を用いると昇順のindex取得が可能である
降順にしたければスライスを使って

>>> A.argsort()[::-1]
array([1, 2, 3, 4, 0])

と取得可能である

54
27
3

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
54
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?