How to sort a list according to another list? Python

后端 未结 4 1553
滥情空心
滥情空心 2021-01-22 09:19

So if I have one list:

name = [\'Megan\', \'Harriet\', \'Henry\', \'Beth\', \'George\']

And I have another list where each value represents the

4条回答
  •  悲哀的现实
    2021-01-22 09:56

    You can sort them at the same time as tuples by using zip. The sorting will be by name:

    tuples = sorted(zip(name, score_list))
    

    And then

    name, score_list = [t[0] for t in tuples], [t[1] for t in tuples]
    

提交回复
热议问题