Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何按降序获得排序数组的索引_Python_Python 3.x_Sorting - Fatal编程技术网

Python 如何按降序获得排序数组的索引

Python 如何按降序获得排序数组的索引,python,python-3.x,sorting,Python,Python 3.x,Sorting,我有一个简单的值列表,我需要索引按原始值从最大到最小的顺序排序 假设清单是 maxList=[7,3,6,9,1,3] 结果应该是: indexedMaxList=[3,0,2,1,5,4] 到目前为止,我尝试的是: def ClusteringOrdermaxList: sortedMaxList=maxList.copy sortedMaxList.sortreverse=True indexedMaxList=[] 对于rangelenmaxList中的i: indexedMaxList.

我有一个简单的值列表,我需要索引按原始值从最大到最小的顺序排序

假设清单是

maxList=[7,3,6,9,1,3] 结果应该是:

indexedMaxList=[3,0,2,1,5,4] 到目前为止,我尝试的是:

def ClusteringOrdermaxList: sortedMaxList=maxList.copy sortedMaxList.sortreverse=True indexedMaxList=[] 对于rangelenmaxList中的i: indexedMaxList.appendmaxList.IndexOrtedMaxList[i] returnindexedmaxList 问题很明显,这样做会返回第一次出现重复值的索引。在这种情况下,双精度3将返回1两次,因此结果为:

indexedMaxList=[3,0,2,1,1,4] 有什么简单的方法可以恢复实际位置吗?

您可以将enumerate与custom key=parameter组合在排序:

印刷品:

[3, 0, 2, 1, 5, 4]
您可以在排序中将enumerate与custom key=参数组合使用:

印刷品:

[3, 0, 2, 1, 5, 4]
输出

[3 0 2 5 1 4]
输出

[3 0 2 5 1 4]
如果您愿意使用numpy:,则可以使用numpy.argsort。在您的情况下:indexedMaxList=np.argsort[7,3,6,9,1,3][::-1][:n]。如果您愿意使用numpy,则可以使用numpy.argsort:。在您的示例中:indexedMaxList=np.argsort[7,3,6,9,1,3][::-1][:n]。tolistAlmost的副本