今天在刷leetcode的时候,对于179题返回最大数,用python2中的sorted(cmp)会很方便,但是在python3中这一参数被取消了,经过查找,发现应该借助functools中的cmp_to_key函数,直接贴代码
import functools
def cmp(a,b):
if a > b :
return -1
elif a < b :
return 1
else:
return 0
nums = [1,2,3,4,5,6]
python 3.4.3 的版本中已经没有cmp函数,被operator模块代替,在交互模式下使用时,需要导入模块。
在没有导入模块情况下,会出现
提示找不到cmp函数了,那么在python3中该如何使用这个函数呢?
所以要导入模块
看下面给的内置函数
operator.lt(a, b)
operator.le(a, b)
operator.eq(a, b)
operator.ne(a, b)
operator.ge(a, b)
operator.gt(a, b)
operato