Python 是否有一个函数将整型列表转换为显示列表从最小到最大顺序的列表?

Python 是否有一个函数将整型列表转换为显示列表从最小到最大顺序的列表?,python,python-3.x,Python,Python 3.x,我需要一个函数,可以像 [10,5,2,3,7] 像这样的列表 [4,2,0,1,3] 基本上是一个列表[0,1,2,3,4…],但按照原始列表的顺序排列,从最小到最大 我甚至不知道从哪里开始这样的函数。我有Python 3.5.2。这个解决方案可以工作;然而,可能有一种更节省空间的解决方案。不过,此解决方案允许重复元素 l1 = [10, 5, 2, 3, 7] # Your list l2 = sorted(l1) # Get the sorted version of our list.

我需要一个函数,可以像 [10,5,2,3,7] 像这样的列表 [4,2,0,1,3]

基本上是一个列表[0,1,2,3,4…],但按照原始列表的顺序排列,从最小到最大


我甚至不知道从哪里开始这样的函数。我有Python 3.5.2。

这个解决方案可以工作;然而,可能有一种更节省空间的解决方案。不过,此解决方案允许重复元素

l1 = [10, 5, 2, 3, 7] # Your list
l2 = sorted(l1) # Get the sorted version of our list.

# A dictionary containing each element and a list of the indices where they are found
element_indices = {}

for index, element in  enumerate(l2):
    if element not in element_indices:
        element_indices[element] = [index] # Store the index for each element when it is sorted
    else: # We have seen this element before
        element_indices[element].append(index)

l2 = [element_indices[value].pop() for value in l1] # Change each element to its sorted equivalent index

print(l2) # [4, 2, 0, 1, 3]

主角希罗的答案很接近,但他编入了错误的列表

>>> data = [10,5,2,3,7]
>>> sorted_list = sorted(data)
>>> [sorted_list.index(item) for item in data]
[4, 2, 0, 1, 3]
这不适用于您想要解释多次事件之类的情况,但我不确定您的情况是否需要这样做。

尝试以下方法:

>>> d = [10, 5, 2, 3, 7]
>>> [sorted(d).index(i) for i in d]
[4, 2, 0, 1, 3]

那么,您想要一个包含原始列表从最小到最大元素索引的列表吗?听起来像是一个面试问题。。。。