我想根据元素第一次出现的索引对列表进行排序
例如:
l=[3,2,1,2,3,1,4]
output=[3,3,2,2,1,1,4]
我尝试了以下代码
def sortfunc(idx:int):
return l.index(idx)
print(sorted(l,key=sortfunc))
它运行得很好,但当我尝试l.sort(key=sortfunc)
它的提升ValueError
>>> l=[3,2,1,2,3,1,4]
>>> l.sort(key=sortfunc)
Traceback (most recent call last):
File "<pyshell#63>", line 1, in <module>
l.sort(key=sortfunc)
File "<pyshell#45>", line 2, in sortfunc
return l.index(idx)
ValueError: 3 is not in list
>>>
我错过了什么
目前没有回答
相关问题 更多 >
编程相关推荐