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,我尝试了以下代码: import numpy s = [6,6,6,6,6] print(numpy.argsort(s)) 输出: [0 1 2 3 4] [2, 3, 4, 0, 1] 预期产出: [4 3 2 1 0] 另一个示例示例代码: s = [5, 5, 2, 2, 3] li = [] for i in range(len(s)): li.append([s[i], i]) li.sort() sort_index = [] for x in li: s

我尝试了以下代码:

import numpy
s = [6,6,6,6,6]
print(numpy.argsort(s))
输出:

[0 1 2 3 4]
[2, 3, 4, 0, 1]
预期产出: [4 3 2 1 0]

另一个示例示例代码:

s = [5, 5, 2, 2, 3]
li = []

for i in range(len(s)):
    li.append([s[i], i])
li.sort()
sort_index = []

for x in li:
    sort_index.append(x[1])

print(sort_index)
输出:

[0 1 2 3 4]
[2, 3, 4, 0, 1]
预期产出:

[3 2 4 1 0]

这种不稳定的排序索引返回在python中是可能的吗?

我认为您需要的是按相反的顺序排序。我说得对吗?已经有一个线程: 功能是
list.sort(reverse=True)
您可以执行以下操作:

>>> s = [5, 5, 2, 2, 3]
>>> sorted(enumerate(s), key=lambda x:(x[1], -x[0]))
[(3, 2), (2, 2), (4, 3), (1, 5), (0, 5)]
如果你只是想要指数

>>> [x[0] for x in sorted(enumerate(s), key=lambda x:(x[1], -x[0]))]
[3, 2, 4, 1, 0]
或者你的另一个例子:

>>> s = [6,6,6,6,6]
>>> [x[0] for x in sorted(enumerate(s), key=lambda x:(x[1], -x[0]))]
[4, 3, 2, 1, 0]

你期望的输出背后的推理是什么?考虑一个场景,所有元素都是不同的,在这种情况下,它应该从原始列表中返回排序元素的索引。当重复元素像[6,6,6,6,6]一样出现时,它应该从最后返回排序列表索引。我想要排序列表的原始索引,但如果存在重复的元素,它应该返回上一个元素的索引。