在python中以正确的顺序向列表中添加元素

在python中以正确的顺序向列表中添加元素,python,Python,我想使用特定索引将元素添加到列表中。在所有情况下,使用insert对我来说都不起作用。请看一看: rest = [666, 555, 222] s= sorted(rest) list1 = [] list1.insert(rest.index(s[0]),3) list1.insert(rest.index(s[1]),2) list1.insert(rest.index(s[2]),1) print list1 所以我想要实现的是——从rest列表中映射到最低值的最高值。 但我

我想使用特定索引将元素添加到列表中。在所有情况下,使用insert对我来说都不起作用。请看一看:

rest = [666, 555, 222]
s= sorted(rest)

list1 = []    
list1.insert(rest.index(s[0]),3)
list1.insert(rest.index(s[1]),2)
list1.insert(rest.index(s[2]),1)

print list1
所以我想要实现的是——从rest列表中映射到最低值的最高值。 但我得到的是: 1,3,2,目标是1,2,3(在本例中)


我知道这就是插入函数的工作原理,但是有没有其他方法可以实现我想要的呢?

这就是您想要的吗

rest = [666, 555, 222]
s= sorted(rest)

list1 = [0] * len(rest)    
list1[rest.index(s[0])] = 3
list1[rest.index(s[1])] = 2
list1[rest.index(s[2])] = 1

print list1

上述代码将
[1,2,3]
作为输出(如您所料)。

如果您想对列表进行永久排序,可以编写:

list1.sort()

这将从最小到最大对列表进行排序。

我已经回答了您的问题。如果有帮助,请接受答案或评论以突出您的问题:)这不是OP想要的。对不起,OP是什么意思?OP=原始海报。a、 k.a提出这个问题的人是我,谢谢你。@Irt最好的感谢方式是接受答案:)