1

Sorted方法排序用法

 

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)]
View Code

 

 

print(sorted([131,1,237,89,100,"22"],key=lambda x:len(str(x))))

#基于长度进行排序 len(strx)作为条件
# [1, 89, '22', 131, 237, 100]
posted @ 2018-12-29 14:45  萌哥-爱学习  阅读(507)  评论(0编辑  收藏  举报