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

I wanted to know the nth largest and smallest value in the list with python, It was a little different from what found article wanted, so I summarized it easily including the part that I thought it would be like this.

First, suppose you have the following list, similar to the original article.

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

ascending order

If you want to get the index in ascending order of this list

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

Will be.

descending order

Next, if you want to get the index in descending order (descending order)

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

In the original article, I implemented descending order using slices, but I tried it with the feeling that there is also this method because it is a little different from the specifications I want.

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
Get the value of a specific key in a list from the dictionary type in the list with Python
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
Try to get a list of breaking news threads 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
Display a list of alphabets in Python 3
[python] Get a list of instance variables
Summary of built-in methods in Python list
Get the EDINET code list in Python
[Python] Get a list of folders only
Get rid of DICOM images in Python
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
Get the caller of a function in Python
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
Sorted list in Python
List of python modules
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 value selected in Selenium Python VBA pull-down
[Python] Get the list of ExifTags names of Pillow library
[Python] Outputs all combinations of elements in the list
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
Measure BMI index in Python.
[python] Value of function object (?)
Shapley value calculation in Python
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 the number of occurrences for each element in the list
[Python] Copy of multidimensional list