How to sort a list according to another list? Python

后端 未结 4 1552
滥情空心
滥情空心 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 10:03

    One obvious approach would be to zip both the list sort it and then unzip it

    >>> name, score = zip(*sorted(zip(name, score_list)))
    >>> name
    ('Beth', 'George', 'Harriet', 'Henry', 'Megan')
    >>> score
    (6, 10, 6, 5, 9)
    

提交回复
热议问题