我想根据另一个列表的值对一个列表的索引进行排序。我的代码是:
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']
我想你的猫掉了。
>>> new = [x[int(index) - 1] for index in y]
>>> new
['mango', 'cat', 'ant', 'pop', 'fog', 'orange', 'butter', 'milk', 'coconut', 'tree', 'sky', 'moon', 'dog']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句