在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]))