python3中已经没有cmp函数,被operator模块代替,在交互模式下使用时,需要导入模块。

在没有导入模块情况下,会出现

python之operator模块

提示找不到cmp函数了,那么在python3中该如何使用这个函数呢?

python之operator模块

#内置函数 
operator.lt(a, b) 
operator.le(a, b) 
operator.eq(a, b) 
operator.ne(a, b) 
operator.ge(a, b) 
operator.gt(a, b) 
operator.__lt__(a, b) 
operator.__le__(a, b) 
operator.__eq__(a, b) 
operator.__ne__(a, b) 
operator.__ge__(a, b) 
operator.__gt__(a, b)   

简单说明下这几个函数的意义:

#函数返回值为布尔型
#在返回 true的情况下
lt(a,b)   表示  a<b :从第一个数字或字母(ASCII)比大小 

le(a,b)   表示  a<=b

eq(a,b)   表示  a==b 字母完全一样,返回True,

ne(a,b)   表示  a!=b

gt(a,b)   表示  a>b

ge(a,b)   表示  a>=b

相关文章:

  • 2021-12-27
  • 2022-12-23
  • 2021-12-11
  • 2022-02-13
  • 2021-09-15
  • 2021-08-01
  • 2022-12-23
  • 2021-12-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-27
  • 2021-08-19
相关资源
相似解决方案