python sort函数 cmp、functools.cmp_to_key 详解

一、sort函数

sort函数默认按从小到大的顺序排列
————————————————
sort()函数原型:
list.sort(key=None, reverse=False)、

(1) key参数
key接受的是一个只有一个形参的函数,形式如下

def f(a):
return len(a)

key接受的函数返回值,表示此元素的权值,sort将按照权值大小进行排序

list = ['aa', 'bbb', 'cccc', 'd']

def compare(a):
    return len(a)

list.sort(key=compare)
print(list)

结果:['d', 'aa', 'bbb', 'cccc']

(2) reverse参数
reverse接受的是一个bool类型的值 (Ture or False),表示是否颠倒排列顺序,一般默认的是False,注意第一个字母是大写的

二、cmp

在python2里面的sort()函数是有三个参数的:
L.sort(cmp=None, key=None, reverse=False)
其中的cmp在python里面已经被删除了,但是还是说明一下

cmp参数:
cmp接受一个函数,拿整形举例,形式为:
def f(a,b):
return a-b

如果排序的元素是其他类型的,
你所定义的这个函数只需满足a逻辑小于b,函数返回负数;a逻辑等于b,函数返回0;a逻辑大于b,函数返回正数就行

cmp()函数本身在python2里是一个内置函数

cmp(42,32)
1
cmp(99,100)
-1
cmp(10,10)
0

————————————————
可参考:https://blog.csdn.net/robinson_/article/details/51484147

三、functools.cmp_to_key

python3 取消了cmp 参数,可以用functools.cmp_to_key 代替

import functools

nums=[3,30,34,5,9]
strs=[str(num) for num in nums]
strs.sort(key=functools.cmp_to_key(lambda x,y:int(x+y)-int(y+x)))

print(''.join(strs))

输出:3033459

比较函数键函数的区别:

比较函数是任何接受两个参数并进行比较的可调用对象,小于返回负数,相等返回零,大于返回正数。

键函数是接受一个参数并返回另一个值作为排序键的可调用对象。

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
The `functools.cmp_to_key` function is a utility function in Python's `functools` module that helps convert a comparison function to a key function. In Python, sorting a list of objects requires a way to compare the objects. This is done by providing a comparison function that takes two objects as input and returns `-1`, `0`, or `1` depending on whether the first object is less than, equal to, or greater than the second object. However, some sorting functions in Python (such as the `sorted` function) require a key function instead of a comparison function. A key function takes a single object as input and returns a value that can be used for sorting. The `cmp_to_key` function helps convert a comparison function to a key function by returning a new function that takes a single object as input and returns a tuple `(comparison_result, object)` where `comparison_result` is the result of calling the original comparison function with the input object and another object, and `object` is the input object itself. This tuple can then be used for sorting. Here's an example of how to use `cmp_to_key`: ``` from functools import cmp_to_key def compare_length(str1, str2): if len(str1) < len(str2): return -1 elif len(str1) > len(str2): return 1 else: return 0 strings = ['cat', 'dog', 'elephant', 'a', 'zebra'] sorted_strings = sorted(strings, key=cmp_to_key(compare_length)) print(sorted_strings) # Output: ['a', 'cat', 'dog', 'zebra', 'elephant'] ``` In this example, we define a comparison function `compare_length` that compares the lengths of two strings. We then use `cmp_to_key` to convert this comparison function to a key function, and pass it to the `sorted` function to sort a list of strings by length. The resulting sorted list is `['a', 'cat', 'dog', 'zebra', 'elephant']`.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值