listA = [3,4,5,3,2,1,] print(sorted(listA)) # [1, 2, 3, 3, 4, 5] listB =["a","z","b","e","l","qq"] print(sorted(listB)) print(sorted(listB ,key=lambda x:x[0])) # [‘a‘, ‘b‘, ‘e‘, ‘l‘, ‘qq‘, ‘z‘] # [‘a‘, ‘b‘, ‘e‘, ‘l‘, ‘qq‘, ‘z‘] listC =[("e",5),("a",4),("b",1),("z",44),("f",1)] print(sorted(listC,key=lambda x:x[0])) print(sorted(listC,key=lambda x:x[1])) #[(‘a‘, 4), (‘b‘, 1), (‘e‘, 5), (‘f‘, 1), (‘z‘, 44)] #[(‘b‘, 1), (‘f‘, 1), (‘a‘, 4), (‘e‘, 5), (‘z‘, 44)]
print(sorted([131,1,237,89,100,"22"],key=lambda x:len(str(x)))) #基于长度进行排序 len(strx)作为条件# [1, 89, ‘22‘, 131, 237, 100]
原文地址:https://www.cnblogs.com/mengbin0546/p/10195948.html
时间: 2024-10-25 12:22:52