Reputation: 758
I know my question seems confusing, but let's say I have the following list:
List1 = [15,16,1,2]
So, the position of [ 15, 16, 1, 2 ]
is [ 0, 1, 2, 3 ]
.
What I am looking for is a result list containing the position from the smallest number of list1 to biggest.
Result List = [2,3,0,1]
I don't exactly know the most efficient way to do it in Python.
Upvotes: 1
Views: 399
Reputation: 1677
What you want is called argsort
in some libraries. In mathematical terms, you want a "permutation" (look it up on wikipedia) that when applied to the input list gives a sorted list.
The best way to do it in Python is the following:
def argsort(lst):
return sorted(range(len(lst)), key=lst.__getitem__)
lst = [15,16,1,2]
print(argsort(lst))
# print: [2, 3, 0, 1]
This is how it works. Instead of sorting the values in lst
, you sort the indexes (range(0, len(lst))
) using the corresponding lst
values as "keys".
Keep in mind that lst.__getitem__(i)
is just equivalent to lst[i]
.
EDIT: for people new to Python and unfamiliar with the "key" argument and the magic methods of Python (those written like __this__
), this may be intimidating. A more friendly versione could be:
def argsort(lst):
indexes = range(len(lst))
return sorted(indexes, key=lambda i: lst[i])
Upvotes: 2
Reputation: 51643
Use enumerate and sorted with a custom key using a lambda expression:
List1 = [15,16,1,2]
v = [i for i,_ in sorted(enumerate(List1), key=lambda x:x[1])]
print(v)
Output:
[2,3,0,1]
enumerate(List1)
produces tuples of (index, value)
that are sorted based on the value
and then only the index
is used to create the resulting list.
Upvotes: 3