0

Sorry that my question may not clear enough and confuse many guys,it is described clearly and there is solution here:https://github.com/numpy/numpy/issues/8757

Given a list a as:

a = [9, 3, 5]

I need the corresponding indices just as:

a_indices = [2, 0, 1]

But when I'm using the method from How can I save the original index after sorting a list?, the output is:

a_indices = [1, 2, 0]

What I need is similar to this:

>>> a = [4, 2, 3, 1, 4]
>>> b = sorted(enumerate(a), key=lambda i: i[1])
[(3, 1), (1, 2), (2, 3), (0, 4), (4, 4)]

The output above is correct but when I assign a as below, the output is unexpected:

>>> a = [9, 3, 5]
>>> b = sorted(enumerate(a), key=lambda i: i[1])
>>> b
[(1, 3), (2, 5), (0, 9)]

The expected output is:

[(2, 3), (0, 5), (1, 9)]

I am confused by the sorting function and anyone could help to explain it to me?

Ye Shiwei
  • 9
  • 5
  • That's exactly what you indicated: `a_indices=[2,0,1]`. The output `b` is similar. – a_guest Apr 24 '21 at 09:25
  • I am really confused. why are you trying to sort it if you want the elements at indices `2,0,1`? – Albin Paul Apr 24 '21 at 09:29
  • But the output is [1,2,0], using the function above – Ye Shiwei Apr 24 '21 at 09:30
  • For `a = [9, 3, 5]` indexes `[2, 0, 1]` correspond to `[5, 9, 3]` values. Since you are sorting the original pairs from `enumarate()` by value, you will never get `[2, 0, 1]` as the output. – accdias Apr 24 '21 at 10:04

6 Answers6

0

You could use enumerate on sorted:

a = [9, 3, 5]
temp = {x: i for i, x in enumerate(sorted(a))}
b = [temp[x] for x in a]
print(b)

Output:

[2, 0, 1]
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
0

There are solution using

np.argsort(np.argsort(a))

reference:https://github.com/numpy/numpy/issues/8757

Ye Shiwei
  • 9
  • 5
0

If your list is [9, 3, 5] then 3 has index 1, not 2; 5 has index 2, not 0; and 9 has index 0, not 1. Remember that the first index of a list is 0, then the second index is 1, etc. So for the sorted list [3, 5, 9] the original indexes are [1, 2, 0] as the output correctly shows. [2, 0, 1] would actually be the list [5, 9, 3] which is not correctly sorted by any sensible rule

workaholic47
  • 151
  • 4
  • The expected result is 2, 0, 1 because those are the indices the elements 9, 3, 5 will move to when the list is sorted. – kaya3 Apr 24 '21 at 10:27
0

Ok now i got your question. What you can do is :

a = [9,3,5] 
b = sorted(enumerate(a), key=lambda i: i[1])    
b = [x for x in enumerate(b)]
b = sorted(b, key=lambda i: i[1][0])
ans = [x[0] for x in b]

output will be [2,0,1]

codotron
  • 51
  • 4
-1

Hey basically enumerate generates tuples of (index,value). So if you run

a=[9,3,5]
print(list(enumerate(a)))

It generates

[(0, 9), (1, 3), (2, 5)]

Indicating that 9 is at 1st position, 3 at second and so on (Python has 0 based indexing). After this comes the custom sort

key=lambda i: i[1]

Says that use the 2nd element in the tuple to sort the list, which is nothing but the numbers present in a.

Now my question is the output given by python is right as per the sorting, but your expected output does not clarify why it should be like that because it doesn't seem you just want to sort. As [2,0,1] will not be out put for 0 based indexing or 1 based indexing.

codotron
  • 51
  • 4
  • 1
    yes, i didnt need sort, what i need is the accoring index position after sorting. It maybe confued but i found a solution from other's question:https://github.com/numpy/numpy/issues/8757 – Ye Shiwei Apr 24 '21 at 09:45
  • Asking for details or clarity on the question (i.e. "why is [2,0,1] your expected result?") is not an answer to the question, so this should be written as a comment instead of an answer. Once you have enough reputation you will be able to write comments. – kaya3 Apr 30 '21 at 13:30
-1

I'm not sure why you expect a different outcome in your example:

>>> list(enumerate(a))
[(0, 9), (1, 3), (2, 5)]

returns 3 tuples, which you are sorting based on the second element in the tuple. In other words, tuples (2, 3), (0, 5), and (1, 9) are not in the original enumeration so you cannot expect to find them after you have sorted the enumeration.

[(1, 3), (2, 5), (0, 9)] is the correct outcome i.e. 3 is the lowest value and the index in the original list was 1 etc.

M-Chen-3
  • 2,036
  • 5
  • 13
  • 34
ibmadar
  • 1
  • 2