Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,按另一个列表的顺序对元组列表进行排序_Python_Python 2.7_Sorting - Fatal编程技术网

在Python中,按另一个列表的顺序对元组列表进行排序

在Python中,按另一个列表的顺序对元组列表进行排序,python,python-2.7,sorting,Python,Python 2.7,Sorting,我有一个元组列表: l1 = [(2, "text1"), (5, "text3"), (7, "text2")] 和另一个字符串列表: l2 = ["text1", "text2", "text3"] 我想按l2的顺序对l1进行排序,以便l1将: [(2, "text1"), (7, "text2"), (5, "text3")] 怎么做 l1.sort(key=lambda x: l2.index(x[1]))

我有一个元组列表:

l1 = [(2, "text1"), (5, "text3"), (7, "text2")]  
和另一个字符串列表:

l2 = ["text1", "text2", "text3"]
我想按l2的顺序对l1进行排序,以便l1将:

[(2, "text1"), (7, "text2"), (5, "text3")]
怎么做

l1.sort(key=lambda x: l2.index(x[1]))