Oge*_*gen 3 python sorting list
l = [0, 1, 3, 2]
l2 = ['foo', 3, 'bar', 10]
Run Code Online (Sandbox Code Playgroud)
如果我说sorted(l),我会得到[0, 1, 2, 3].它将交换最后两个元素.
如何应用相同的行交换l2?即,我想l2成为['foo', 3, 10, 'bar'].
您可以使用zip,解包元组和列表推导来实现结果:
[y for x, y in sorted(zip(l, l2))]
Run Code Online (Sandbox Code Playgroud)