python中根据某个list排序另一个list

https://s tackoverflow.com

/questions/6618515/sorting-list-based-on-values-from-another-list

比如有如下两个list:

people =['Jim','Pam','Micheal','Dwight']

ages =[27,25,4,9]

按ages排序

方法一:

[x for _,x in sorted(zip(ages,people))]

方法二:

import numpy as np

people=np.array(people)

ages=np.array(ages)

indx=ages.argsort()

sortedpeople=people[indx]

方法三:

sorted_index=sorted(range(len(age)),key=lambda x:people[x])

[people[i] for i in sorted_index]



顺便鄙视一下百度和墙。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注