Getting indices of ascending order of list

后端 未结 2 813
忘掉有多难
忘掉有多难 2021-01-16 13:33

I know that this question has been asked a hundred times, but the answer always seems to be \"use numpy\'s argsort\". But either I am misinterpreting what most people are as

相关标签:
2条回答
  • 2021-01-16 14:01

    The reason why you are not getting the 'right,' or expected, answer is because you are asking the wrong question!

    What you are after is the element rank after sort while Numpy's argsort() returns the sorted index list, as documented!. These are not the same thing (as you found out ;) )!

    0 讨论(0)
  • 2021-01-16 14:07

    The argsoft() is basically converting your list to a sorted list of indices.

    l = [4, 2, 1, 3]
    

    First it gets index of each element in the list so new list becomes:

    indexed=[0, 1, 2, 3]
    

    Then it sorts the indexed list according to the items in the original list. As 4:0 , 2:1 , 1:2 and 3:3 where : means "corresponds to".

    Sorting the original list we get

    l=[1, 2, 3, 4]
    

    And placing values of each corresponding index of old list

    new=[2,1,3,0]
    

    So basically it sorts the indices of a list according to the original list. This is as far as i understood. Sorry if i am wrong.

    0 讨论(0)
提交回复
热议问题