我想根據另一個列表的值排序一個列表的索引。我的代碼是:根據另一個列表值對列表的索引值進行排序
x = ['mango','orange','butter','milk','coconut','tree','sky','moon','dog','cat','ant','pop','fog'] // sort this list
y = ['1','10','11','12','13','2','3','4','5','6','7','8','9']
什麼我做的是:
>>> x.sort(key=lambda (a,b): y.index(a))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
ValueError: too many values to unpack
我期望的結果是:
x = ['mango','cat','ant', 'pop', 'fog','orange','butter','milk','coconut','tree','sky','moon','dog']
..這真的是你想要的結果嗎? – DSM
你想要的輸出對我來說不對。嘗試:'zip(* sorted(zip(x,y),key = lambda z:int(z [1])))[0]' –
是的,這是我期望的結果,'y'表示的索引號 - 1. – user227666