[PYTHON] Extract array elements and indexes in descending order with numpy

Use the sort and argsort functions to retrieve array elements and indexes in descending order with numpy.

However, since the sort function sorts in ascending order and the argsort function returns an array of indexes sorted in ascending order, use the slice writing method [:: -1] to reverse the order, that is, in descending order.

python


import numpy as np

x = np.array([18, 7, 55, 31])

for i in range(len(x)):
    print np.argsort(x)[::-1][i], np.sort(x)[::-1][i]

The output is as follows.

2 55
3 31
0 18
1 7

Recommended Posts

Extract array elements and indexes in descending order with numpy
Extract multiple elements with Numpy array
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
Various ways to extract columns in a NumPy array
Calculate Entropy for arrays with zero elements in Numpy
Extract elements other than a specific index with Numpy
Extract non-numeric elements with pandas.DataFrame
How to create dataframes and mess with elements in pandas
About Numpy array and asarray
Preserve and read order in PyYAML
Invert numpy Boolean array in tilde
Extract the Azure SQL Server data table with pyodbc and try to make it numpy array / pandas dataframe
Dealing with "years and months" in Python
Graph trigonometric functions with numpy and matplotlib
Differences between Numpy 1D array [x] and 2D array [x, 1]
[Python] Combine all the elements in the array
Function to extract the maximum and minimum values ​​in a slice with Go
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
Extract the color of the object in the image with Mask R-CNN and K-Means clustering