Python2/3 sort和sorted函数

定义

sort 是 Python list 内置的排序方法;直接在原列表上进行排序,无返回值
sorted 是 Python 的全局排序方法,可对所有可迭代的对象进行排序操作;原对象不变,返回一个排序后的新列表。

参数说明:

iterable -- 可迭代对象。

cmp -- 比较的函数,这个具有两个参数,参数的值都是从可迭代对象中取出,此函数必须遵守的规则为,大于则返回1,小于则返回-1,等于则返回0。

key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。

reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。

sort 函数

  1. 函数原型:
# Python2
sort(...)
    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    cmp(x, y) -> -1, 0, 1

# Python3
sort(*, key=None, reverse=False) method of builtins.list instance
    Sort the list in ascending order and return None.

    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
    order of two equal elements is maintained).

    If a key function is given, apply it once to each list item and sort them,
    ascending or descending, according to their function values.

    The reverse flag can be set to sort in descending order.
  1. 使用范例:
# 列表简单排序
>>> a = [1, 2, 5, 3, 9, 4, 6, 8, 7, 0, 12]
>>> a.sort()
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
>>> a.sort(reverse = True) # 默认 reverse = False ,升序排列;reverse = True 代表降序排列。
>>> a
[12, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# 列表复杂排序
>>> list1 = [(8, 'Logan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
>>> list1.sort()
>>> list1
[(2, 'Mike', 22), (5, 'Lucy', 19), (8, 'Logan', 20)]
>>> list1.sort(key = lambda x: x[2])
>>> list1
[(5, 'Lucy', 19), (8, 'Logan', 20), (2, 'Mike', 22)]

# Python2 特例 --- cmp 参数的使用
>>> L = [('b', 2), ('a', 1), ('c', 3), ('d', 4)]
>>> L.sort(cmp = lambda x, y : cmp(x[1], y[1]))
>>> L
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

sorted 函数

  1. 函数原型:
# Python2
sorted(...)
    sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

# Python3

sorted(iterable, /, *, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.

    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.

  1. 使用范例:
# 简单排序
>>> a = [1, 2, 5, 3, 9, 4, 6, 8, 7, 0, 12]
>>> sorted(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
>>> sorted(a, reverse = True)# 默认 reverse = False ,升序排列;reverse = True 代表降序排列。
[12, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# 复杂排序
>>> list1 = [(8, 'Logan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
>>> sorted(list1)
[(2, 'Mike', 22), (5, 'Lucy', 19), (8, 'Logan', 20)]
>>> sorted(list1, key = lambda x: x[2])
[(5, 'Lucy', 19), (8, 'Logan', 20), (2, 'Mike', 22)]

# Python2 特例 --- cmp 参数的使用
>>> def my_cmp(x, y):
...     return x - y
...
>>> L = [('b', 2), ('a', 1), ('c', 3), ('d', 4)]
>>> sorted(L, cmp = lambda x, y: my_cmp(x[1], y[1]))
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

Python2 与 Python3 区别

  1. key和reverse必须作为关键字参数传递,不像Python 2中,它们可以作为位置参数传递。

  2. Python3 中 [].sort() 和 sorted() 均取消了 cmp 参数。相反,仅 key 用于引入自定义排序逻辑。
    此时如果还需要使用自定义的比较函数,那么可以使用 functools.cmp_to_key() 函数,该函数主要用来将老式的比较函数(comparison function)转化为关键字函数(key function),因为 Python 3 中不支持比较函数。

以 sorted 函数为例,cmp_to_key 函数的用法如下:

# 简单使用
>>> a = [1, 2, 5, 3, 9, 4, 6, 8, 7, 0, 12]
>>> def reverse_cmp(x, y):
...     return y - x
...
>>> sorted(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
>>> sorted(a, key = cmp_to_key(reverse_cmp))
[12, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]


# 复杂情况
# 可使用 lambda 进一步规定 key 位置
>>> from functools import cmp_to_key
>>> def my_cmp(x, y):
...     return x - y
...
>>> L = [('b', 2), ('a', 1), ('c', 3), ('d', 4)]
>>> sorted(L, key = cmp_to_key(lambda x, y: my_cmp(x[1], y[1])))
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

# 或直接在函数中处理
>>> from functools import cmp_to_key
>>> def my_cmp(x, y):
...     return x[1] - y[1]
...
>>> L = [('b', 2), ('a', 1), ('c', 3), ('d', 4)]
>>> sorted(L, key = cmp_to_key(my_cmp))
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值