網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
python3 cmp實(shí)現(xiàn)
python3移除了cmp()函數(shù),但提供了六個(gè)豐富的比較運(yùn)算符,詳見此處
import operator ? ? ? #首先要導(dǎo)入運(yùn)算符模塊 operator.gt(1,2) ? ? ?#意思是greater than(大于) operator.ge(1,2) ? ? ?#意思是greater and equal(大于等于) operator.eq(1,2) ? ? ?#意思是equal(等于) operator.le(1,2) ? ? ?#意思是less and equal(小于等于) operator.lt(1,2) ? ? ?#意思是less than(小于)
PY3__cmp__ mixin類
import sys PY3 = sys.version_info[0] >= 3 if PY3: def cmp(a, b): return (a > b) - (a < b) # mixin class for Python3 supporting __cmp__ class PY3__cmp__: def __eq__(self, other): return self.__cmp__(other) == 0 def __ne__(self, other): return self.__cmp__(other) != 0 def __gt__(self, other): return self.__cmp__(other) > 0 def __lt__(self, other): return self.__cmp__(other) < 0 def __ge__(self, other): return self.__cmp__(other) >= 0 def __le__(self, other): return self.__cmp__(other) <= 0 else: class PY3__cmp__: pass class YourClass(PY3__cmp__): '''自定義類,可以用list.sort函數(shù)或者sorted函數(shù)來(lái)實(shí)現(xiàn)排序。''' def __init__(self, name, age): self.name = name self.age = age def __cmp__(self, other): return cmp(self.age, other.age)
cmp()函數(shù)實(shí)現(xiàn)的注解
bool僅僅是一個(gè)int子類,那么True和False可以理解為1和0區(qū)別。
因?yàn)槿绻谝粋€(gè)參數(shù)小于第二個(gè)參數(shù),cmp返回負(fù)值,如果參數(shù)相等則返回0,否則返回正值,可以看到False - False == 0,True - False == 1和False - True == -1為cmp提供正確的返回值。
python3 使用cmp函數(shù)報(bào)錯(cuò)
python3中已經(jīng)不使用cmp函數(shù)進(jìn)行比較大小
使用operator模塊
import operator lt(a,b) 相當(dāng)于 a<b ? ? 從第一個(gè)數(shù)字或字母(ASCII)比大小 ? le(a,b)相當(dāng)于a<=b? eq(a,b)相當(dāng)于a==b ? ? 字母完全一樣,返回True,? ne(a,b)相當(dāng)于a!=b? gt(a,b)相當(dāng)于a>b? ge(a,b)相當(dāng)于 a>=b
函數(shù)的返回值是布爾哦
原文鏈接:https://blog.csdn.net/A156348933/article/details/86686190
相關(guān)推薦
- 2022-04-26 Android?Jetpack?Compose實(shí)現(xiàn)列表吸頂效果_Android
- 2023-01-10 Flutter?CustomPaint繪制widget使用示例_IOS
- 2022-04-17 python使用openpyxl讀取日期并修改excel
- 2022-09-20 C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之單鏈表操作詳解_C 語(yǔ)言
- 2022-08-01 Python+Selenium鍵盤鼠標(biāo)模擬事件操作詳解_python
- 2024-03-01 解決 “TypeError: Cannot read properties of undefined
- 2022-07-30 一文掌握Python正則表達(dá)式_python
- 2022-12-29 Kotlin?Service服務(wù)組件開發(fā)詳解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支