Get index of nth largest / smallest value in list in Python

When doing list operations, I often want an index up to the nth largest value. Until now, I kneaded it appropriately, but I knew that using numpy would make it smarter, so I made a note for myself.

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

When there was a list

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

It is possible to get the index in ascending order by using argsort () for and ndarray. If you want to descend, use slices

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

Can be obtained

Recommended Posts

Get index of nth largest / smallest value in list in Python
Get index of nth largest / smallest value in list in Python
Get the value of a specific key up to the specified index in the dictionary list in Python
How to retrieve the nth largest value in Python
[python] Get the list of classes defined in the module
Get the number of specific elements in a python list
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Get the index of each element of the confusion matrix in Python
[python] Get the rank of the values in List in ascending / descending order
Get a list of files in a folder with python without a path
[python] Get a list of instance variables
Get the EDINET code list in Python
[Python] Get a list of folders only
Get a list of packages installed in your current environment with python
I measured 6 methods to get the index of the maximum value (minimum value) of the list
[Python] Sort the list of pathlib.Path in natural sort
Make a copy of the list in Python
Find the divisor of the value entered in python
Get a glimpse of machine learning in Python
Search by the value of the instance in the list
Python> list> pop ()> Get index {specified / unspecified} + del ()
[Python] Summary of functions that return the index that takes the closest value in the array
How to get a list of files in the same directory with python
List of python modules
Filter List in Python
List find in Python
Get date in Python
How to get the number of digits in Python
[Python] Manipulation of elements in list (array) [Add / Delete]
Get the size (number of elements) of UnionFind in Python
Get the value selected in Selenium Python VBA pull-down
List of Python code used in big data analysis
[Python] Get the list of ExifTags names of Pillow library
Python: Get a list of methods for an object
Group by consecutive elements of a list in Python
Get the URL of the HTTP redirect destination in Python
How to identify the element with the smallest number of characters in a Python list?
Get YouTube Comments in Python
Try to get the function list of Python> os package
Summary of Python3 list operations
Get last month in python
Python> Get a list of files in multiple directories> Use glob | Sort by modification time
Shapley value calculation in Python
Get a list of purchased DMM eBooks with Python + Selenium
List of nodes in diagrams
Developed a library to get Kindle collection list in Python
Equivalence of objects in Python
Since Python 1.5 of Discord, I can't get a list of members
Get the value while specifying the default value from dict in Python
Get Terminal size in Python
Decrypt one line of code in Python lambda, map, list
Explicitly get EOF in python
I tried to create a Python script to get the value of a cell in Microsoft Excel
Get Evernote notes in Python
Implementation of quicksort in Python
Getting list elements in Python
Python> dictionary> get ()> optional value
Get Japanese synonyms in Python
python memo: enumerate () -get index and element of list at the same time and turn for statement
I tried to get the index of the list using the enumerate function