python3.x和python2.x的sorted()函数的区别:
sorted(iterable, cmp=None, key=None,reverse=False) ## Python 2.x
sorted(iterable, key=None,reverse=False) ## Python 3.x
## 由此可看出,Python3.x取消了 cmp参数, 所以如果想给 sorted()函数传参的话,就剩下 key,和reverse了。
也就是说我们无法直接传入多个参数的函数进行排序
如果直接写的话,会发现
## 报错信息如下:
TypeError: must use keyword argument
for
key function
类型错误: 必须使用关键字参数。
那么我们该如何解决这个问题呢?
那就是使用functools中的cmp_to_key,即在开头加上
from functools import cmp_to_key
代码:
结果: