小编nor*_*.22的帖子

如何获取按降序排列的数组的索引

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

我们假设列表是

maxList = [7, 3, 6, 9, 1, 3]
Run Code Online (Sandbox Code Playgroud)

结果应该是:

indexedMaxList = [3, 0, 2, 1, 5, 4]
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试过的:

def ClusteringOrder(maxList):
    sortedMaxList = maxList.copy()
    sortedMaxList.sort(reverse=True)
    indexedMaxList = []
    for i in range(len(maxList)):
        indexedMaxList.append(maxList.index(sortedMaxList[i]))
    return(indexedmaxList)
Run Code Online (Sandbox Code Playgroud)

问题很明显,这样做会返回第一次出现重复值的索引。在这种情况下,双 3 将返回 1 两次,因此结果将是:

indexedMaxList = [3, 0, 2, 1, 1, 4]
Run Code Online (Sandbox Code Playgroud)

有没有什么简单的方法可以恢复实际位置?

python sorting python-3.x

5
推荐指数
2
解决办法
5060
查看次数

标签 统计

python ×1

python-3.x ×1

sorting ×1