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.
np.array([4,7,9,10,6,11,3]).argsort()
givesarray([6, 0, 4, 1, 2, 3, 5])
, which is by decreasing order. (Your use-case wants to rank by increasing order, in which case just negate the array before argsort'ing). NumPy is faster than native Python and mostly in C. \$\endgroup\$rank()
\$\endgroup\$