Desired Output
I want a function to return a list such that, given a "jumbled" list l
, each element is the index of the corresponding element of l
, if l
was sorted. (I'm failing to think of a less convoluted way of saying this, sorry.)
Examples
f([3,1,2])
= [2,0,1]
f([3,1,2,2,3])
= [3,0,1,2,4]
, since the input sorted is [1,2,2,3,3]
.
(This is useful for some stats calculations.)
My Attempt
I came up with a way to do this function, but this is python- it seems like there should be a one-liner to do this, or at least a much cleaner, clearer way.
def getIndiciesInSorted(l):
sortedL = sorted(l)
outputList = []
for num in l:
sortedIndex = sortedL.index(num)
outputList.append(sortedIndex)
sortedL[sortedIndex] = None
return outputList
l=[3,1,2,2,3]
print getIndiciesInSorted(l)
So, how can I write this more concisely? Is there a legible list comprehension solution?