python3中cmp如何实现
发表于:2024-10-21 作者:千家信息网编辑
千家信息网最后更新 2024年10月21日,这篇文章主要为大家展示了"python3中cmp如何实现",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"python3中cmp如何实现"这篇文章吧。pyth
千家信息网最后更新 2024年10月21日python3中cmp如何实现
这篇文章主要为大家展示了"python3中cmp如何实现",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"python3中cmp如何实现"这篇文章吧。
python3 cmp实现
python3移除了cmp()函数,但提供了六个丰富的比较运算符,详见此处
import operator #首先要导入运算符模块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 sysPY3 = sys.version_info[0] >= 3if 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) <= 0else: class PY3__cmp__: passclass YourClass(PY3__cmp__): '''自定义类,可以用list.sort函数或者sorted函数来实现排序。''' def __init__(self, name, age): self.name = name self.age = age def __cmp__(self, other): return cmp(self.age, other.age)
cmp()函数实现的注解
bool仅仅是一个int子类,那么True和False可以理解为1和0区别。
因为如果第一个参数小于第二个参数,cmp返回负值,如果参数相等则返回0,否则返回正值,可以看到False - False == 0,True - False == 1和False - True == -1为cmp提供正确的返回值。
python3 使用cmp函数报错
python3中已经不使用cmp函数进行比较大小
使用operator模块
import operatorlt(a,b) 相当于 ab ge(a,b)相当于 a>=b
函数的返回值是布尔哦
以上是"python3中cmp如何实现"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
函数
意思
内容
参数
篇文章
大小
模块
运算符
学习
帮助
运算
子类
字母
布尔
数字
易懂
更多
条理
注解
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器能不能无线上网
数据库系统工程师论坛
后端从数据库取数返回给前端
nba2k22连接不上服务器错误代码
万德wind金融经济数据库
ipad设置代理服务器
数据库系统工程师实习期
视频网络技术的发展背景
安徽机械软件开发项目信息
数据库几种事务
广州智慧电梯软件开发费用
大学生应对网络安全的措施
数据库 云备份
第五人格开发者服务器位置
fifa足球数据库
儿童电话手表服务器出错
机构树的数据库表设计图
软件开发是不是加班很多
创建名字为mydb的数据库代码
服务器agent版本怎么看
事业单位网络安全方案
饥荒服务器所有者必须
数据库介绍
服务器 需要什么
国家网络安全产业规划
家用电脑有用服务器机箱的没有
湖北网络安全公司排名
jsp数据库pdf下载
常用的网络安全关键技术的内容
租个服务器大概多少钱