Sorting list based on values from another list?

后端 未结 15 2335
温柔的废话
温柔的废话 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 08:01

    Also, if you don't mind using numpy arrays (or in fact already are dealing with numpy arrays...), here is another nice solution:

    people = ['Jim', 'Pam', 'Micheal', 'Dwight']
    ages = [27, 25, 4, 9]
    
    import numpy
    people = numpy.array(people)
    ages = numpy.array(ages)
    inds = ages.argsort()
    sortedPeople = people[inds]
    

    I found it here: http://scienceoss.com/sort-one-list-by-another-list/

提交回复
热议问题