Thursday, 15 April 2010

python - The meaning of key function in sort() and the meaning of np.argsort() -


I want to sort a list and want to get its index returns.

So I did it in two ways, but both of them gave me wrong results. I do not know what's the problem in it

For example:

  myList = [2, 1, 7, 3, 6]  

My goal is to get the result: [1, 0, 4, 2, 3]

then the first method:

  np.array (myList) .argsort  

But I found the wrong result:

  out of the array ([1, 0, 3, 4, 2])  < / Pre> 

The second method:

  index = category [5] IndiesSortort. (Key = myList .__getitem__)  

I found the same wrong result:

  outside: [1, 0, 3, 4, 2] < / Code> 

I do not know why this happens?

here sorts your original list by myList your expectations list You have two results:

Discounts:

  & gt; & Gt; & Gt; Results from your solutions: [1, 0, 4, 2, 3]] [1, 2, 6, 7, 3]  

Results from your solutions:

  Gt; & Gt; & Gt; [1, 0, 3, 4, 2]] [1, 2, 3, 6, 7]  

I think your solutions are correct and your required list is incorrect.

Update

On the interpretation of the expected result, the base is actually the "rank" of each item in the input, the solution here: [2, 1, 7 , 3, 6] def get_rankings (list): return [sort (list). Index for each of the listings (each)] print get_rankings (MyList) # [1, 0, 4, 2, 3]


No comments:

Post a Comment