Sorting list based on values from another list?

后端 未结 15 2333
温柔的废话
温柔的废话 2020-11-21 07:14

I have a list of strings like this:

X = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\"]
Y = [ 0,   1,   1,   0,   1,   2,   2,   0,   1 ]
         


        
15条回答
  •  一向
    一向 (楼主)
    2020-11-21 07:56

    I actually came here looking to sort a list by a list where the values matched.

    list_a = ['foo', 'bar', 'baz']
    list_b = ['baz', 'bar', 'foo']
    sorted(list_b, key=lambda x: list_a.index(x))
    # ['foo', 'bar', 'baz']
    

提交回复
热议问题