首页 > 其他分享 >how to sort list according to the number/another list

how to sort list according to the number/another list

时间:2022-12-11 20:35:19浏览次数:76  
标签:sort key random list according number another

20221111

Sort with custom function using key

list.sort(key=len)
or
sorted(list, key=len)

# take second element for sort
def takeSecond(elem):
    return elem[1]

# random list
random = [(2, 2), (3, 4), (4, 1), (1, 3)]

# sort list with key
random.sort(key=takeSecond)

# print list
print('Sorted list:', random)

Sort with another list
[x for _, x in sorted(zip(Y, X))]

Reference:
[1] https://www.programiz.com/python-programming/methods/list/sort
[2] https://stackoverflow.com/questions/6618515/sorting-list-based-on-values-from-another-list

标签:sort,key,random,list,according,number,another
From: https://www.cnblogs.com/xiaoxu-xli/p/16974355.html

相关文章