在不对输出索引列表进行排序的情况下按从最大值到最小值的顺序获取索引并修改另一个列表

Get the index in order of values from max to min without sorting the output index list and amend another list as well

我试图在不对索引位置进行排序的情况下,从最大值到最小值获取数组中值的索引。这是我目前的代码

s = [20, 30, 45, 14, 59]
ans = sorted(range(len(s)), key=lambda k: s[k], reverse=True)
print ans

但我得到的输出是 [4, 2, 1, 0, 3],这是基于排序的。我不想要排序的索引列表,而是希望它们处于相同的位置。输出应为 [3, 2, 1, 4, 0]。有什么办法可以实现吗?

对于 2D:现在如果有另一个数组(仅与 -1、0、1 相关联)与第一个数组关联 sv = [(1,0), (-1,1), (-1,0), (0,-1), (1,1)]。这里 sv 中的每个值都被索引并绑定到相同的输出数组 [3, 2, 1, 4, 0]。现在根据特定条件说

for i in s if any val[i] < max(val[i])*25/100

值将从 s 以及 sv 及其索引中删除。所以在上面的例子中,new s、new sv 和 new indexing 输出将是

s = [20, 30, 45, 59]
indexing = [3, 2, 1, 0]
sv = [(1,0), (-1,1), (-1,0), (1,1)]

您可以使用将每个列表项映射到其排序索引的字典:

mapping = {k: i for i, k in enumerate(sorted(s, reverse=True))}
print([mapping[i] for i in s])

这输出:

[3, 2, 1, 4, 0]
s = [20, 30, 45, 14, 59]
ss = sorted(s,reverse=True)


print([s.index(i) for i in ss]) ## gives [4, 2, 1, 0, 3]
print([ss.index(i) for i in s]) ## gives [3, 2, 1, 4, 0]