Skip to main content
edited tags; edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Creating a list containing the orderingrank of the elements in the original list

python - Creating a list containing the ordering of the elements in the original list

Source Link
L1meta
  • 243
  • 1
  • 2
  • 5

python - Creating a list containing the ordering of the elements in the original list

I don't really know how to explain what I'm looking for in a way that makes sense, but here goes:

Say I have a list $$L=(4,7,9,10,6,11,3)$$ What I want to produce is a corresponding list $$ K = (1,3,4,5,2,6,0)$$

where element K[i] has the value of the 'rank' of the element for the corresponding location in L. So the higher the number the higher the rank, starting from rank 0 for the smallest number in L

The code I have written for this is:

x = [4,7,9,10,6,11,3]
index = [0]*len(x)

for i in range(len(x)):
    index[x.index(min(x))] = i
    x[x.index(min(x))] = max(x)+1

and it works, but I just feel it looks horrible, and was wondering if there might exist a more aesthetic way.