在python中使用argsort进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19804091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:41:15  来源:igfitidea点击:

Sort using argsort in python

pythonpython-2.7numpy

提问by user2958912

I try to sort an array:

我尝试对数组进行排序:

import numpy as np

arr = [5,3,7,2,6,34,46,344,545,32,5,22]
print "unsorted"
print arr

np.argsort(arr)

print "sorted"
print arr

But the output is:

但输出是:

unsorted
[5, 3, 7, 2, 6, 34, 46, 344, 545, 32, 5, 22]
sorted
[5, 3, 7, 2, 6, 34, 46, 344, 545, 32, 5, 22]

The array does not change at all

数组根本不改变

采纳答案by askewchan

There are two issues here; one is that np.argsortreturns an array of the indiceswhich would sort the original array, the second is that it doesn't modify the original array, just gives you another. This interactive session should help explain:

这里有两个问题;一个是np.argsort返回一个索引数组,它可以对原始数组进行排序,第二个是它不会修改原始数组,只是给你另一个。这个交互式会话应该有助于解释:

In [59]: arr = [5,3,7,2,6,34,46,344,545,32,5,22]

In [60]: np.argsort(arr)
Out[60]: array([ 3,  1,  0, 10,  4,  2, 11,  9,  5,  6,  7,  8])

Above, the [3, 1, 0, ...]means that item 3in your original list should come first (the 2), then item 2should come (the 3), then the first (index is 0, item is 5) and so on. Note that arris still unaffected:

上面的[3, 1, 0, ...]意思是3原始列表中的 item应该先出现 (the 2),然后 item2应该出现 (the 3),然后是第一个 (index is 0,item is 5),依此类推。请注意,arr仍然不受影响:

In [61]: arr
Out[61]: [5, 3, 7, 2, 6, 34, 46, 344, 545, 32, 5, 22]

You might not need this array of indices, and would find it easier to just use np.sort:

您可能不需要这个索引数组,并且会发现使用它更容易np.sort

In [62]: np.sort(arr)
Out[62]: array([  2,   3,   5,   5,   6,   7,  22,  32,  34,  46, 344, 545])

But this still leaves arralone:

但这仍然是arr单独的:

In [68]: arr
Out[68]: [5, 3, 7, 2, 6, 34, 46, 344, 545, 32, 5, 22]

If you want to do it in place (modify the original), use:

如果你想就地做(修改原来的),使用:

In [69]: arr.sort()

In [70]: arr
Out[70]: [2, 3, 5, 5, 6, 7, 22, 32, 34, 46, 344, 545]

回答by TerryA

np.argsortdoesn't sort the list in place, it returns a list full of indicies that you are able to use to sort the list.

np.argsort不会就地对列表进行排序,它会返回一个完整的索引列表,您可以使用这些索引对列表进行排序。

You must assign this returned list to a value:

您必须将此返回的列表分配给一个值:

new_arr = np.argsort(arr)

Then, to sort the list with such indices, you can do:

然后,要使用此类索引对列表进行排序,您可以执行以下操作:

np.array(arr)[new_arr]

回答by Phil Cooper

Try

尝试

order = np.argsort(arr)
print np.array(arr)[order]

the argsortresponse is the index of the elements.

所述argsort响应是元素的索引。

回答by JoshAdel

If you want your array sorted in-place you want arr.sort():

如果你想你的数组就地排序,你想要arr.sort()

In [1]: import numpy as np  
In [2]: arr = [5,3,7,2,6,34,46,344,545,32,5,22]

In [4]: print arr
[5, 3, 7, 2, 6, 34, 46, 344, 545, 32, 5, 22]

In [5]: arr.sort()
In [7]: print arr
[2, 3, 5, 5, 6, 7, 22, 32, 34, 46, 344, 545]