【python基础】sort函数

1. 基本用法

1)仅对于list类型的数据

a.sort() 按升序

a.sort(reverse = True) 按降序

2)对于所有可排序类型的数据

sorted(a, reverse = True)

2. 自定义排序函数

a = ((1,2), (3,1), (2,7))
b = sorted(a, key=lambda a: a[1])# b = [(3, 1), (1, 2), (2, 7)]

3. sort内核

python的内置函数sort实际上是应用了归并排序的思想实现的。

猜你喜欢

转载自blog.csdn.net/u013166817/article/details/83748880