Python,按另一个列表对列表进行排序 [英] Python, sort a list by another list
本文介绍了Python,按另一个列表对列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表a
:
a = ['c','d','b','a','e']
和一个列表b
:
b = ['a001','b002','c003','d004','e005']
以及如何获得我的列表 c
如下:
and how could I get my list c
as following:
c = ['c003','d004','b002','a001','e005']
基本上使用每个元素的一部分对 b
进行排序,按照 a
中定义的顺序.
Basically sort b
using part of each element, by the order defined in a
.
非常感谢.
推荐答案
您可以尝试将 lambda
函数传递给 sorted() 的
内置函数:key
参数
You can try passing a lambda
function to the key
parameter of the sorted()
built-in function:
a = ['c', 'd', 'B', 'a', 'e']
b = ['a001', 'B002', 'c003', 'd004', 'e005']
c = sorted(b, key = lambda x: a.index(x[0])) # ['c003', 'd004', 'b002', 'a001', 'e005']
这篇关于Python,按另一个列表对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文