Desired output
I want the function to return the list in such a way that, given the "confused" list l , each element is the index of the corresponding element l if l was sorted. (I'm not thinking of a less confusing way of saying this, sorry.)
<strong> Examples
f([3,1,2]) = [2,0,1]
f([3,1,2,2,3]) = [3,0,1,2,4] , since the input is sorted [1,2,2,3,3] .
(This is useful for some statistical calculations.)
My attempt
I came up with a way to execute this function, but it's python - it looks like there should be a one-line or at least a much cleaner and cleaner way for this.
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 briefly? Is there a clear understanding of understanding the text?
source share