Home > Back-end >  How to generate an array in Python which shows the positions of the highest to the lowest integers o
How to generate an array in Python which shows the positions of the highest to the lowest integers o

Time:07-21

I'd like to generate an array which contains the positions of the highest integers/floating point numbers to the lowest in another array. For example:

integers = [1,6,8,5] I want the newly generated array to be: newArray = [2,1,3,0]

or

floatingPoints = [1.6,0.5,1.1] would become newArray = [0,2,1]

CodePudding user response:

You can use the numpy function argsort and then simply reverse the ordering as it gives you ascending rather than descending, by default:

np.argsort(integers)[::-1]

Example:

import numpy as np
integers = np.array([1, 6, 8, 5])
np.argsort(integers)[::-1]

This results in the desired [2, 1, 3, 0].

CodePudding user response:

What I'd do is simply sort the array in descending order using

sorted = integers.sort()

(see doc) and then find the index of all the elements of the sorted array, to do that have a look at this answer.

  • Related