Sorting list based on values from another list?

后端 未结 15 2352
温柔的废话
温柔的废话 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:49

    You can create a pandas Series, using the primary list as data and the other list as index, and then just sort by the index:

    import pandas as pd
    pd.Series(data=X,index=Y).sort_index().tolist()
    

    output:

    ['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']
    

提交回复
热议问题