按顺序排序其他两个列表中的列表



我有一个有趣的问题,我还找不到任何解决方案。具有两个以不同顺序为相同值的列表。我重新排序一个列表,以使列表变得相同。很好,但是我还需要重新排序的第三个列表,其中具有相同的密钥。

我无法在代码中使用ZIP或枚举ZIP来检索第二个列表的新索引。

first=[(1,1,1),(2,2,2),(3,3,3)]
second=[(2,2,2),(3,3,3),(1,1,1)]
third=[2,3,1]
second=sorted(second,key=first.index)
print(second)

[(1,1,1),(2,2,2),(3,3,3)]

d = [i[0] for i in sorted(enumerate(second), key=lambda x:first.index)]

d应该是 (2,0,1),但不起作用

第三列表的目标是成为:[1,2,3]

获取[1, 2, 3],使用:

third = sorted(third, key=list(map(lambda x: x[0],first)).index)

获得[2, 0, 1],使用:

d = sorted(range(second), key=lambda x: first[x])

解决了这个问题。可以从链接上显示的重新排序的"第二"中获取索引,并使用此索引列表对任何其他列表进行排序。

按其他列表的顺序对python列表进行排序,然后检索旧索引

最新更新