python之operator模块


python3中已经没有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) 
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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM