cmp函数python python cmap

python compare函数

cmp是python的内建函数.

创新互联公司-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、绵阳服务器托管、等保安全、私有云建设等企业级互联网基础服务,欢迎咨询:18982081108

cmp(x,y) 用于 compare x 和 y的值.

sort(cmp)只是用于说明,python中函数也是可以作为参数传入其他函数来进行调用的,排序的依据就是cmp.

Python3.x运用cmp()函数进行高级排序报错

numbers.sort这种用法是错误的,如果你想要排序,则用如下语句:

num_sort=sorted(numbers,key=None,reverse=False)

新的list num_sort才是一个排序后的列表。然后,你自定义的cmp过程只能对比两个数字,而能对比列表中的各个元素,python3解释器不知道你要做什么,所以才会出错。

python3.2.2版本中的cmp()函数

3开始没这个函数了,官方文档是这么写的

The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a b) - (a b) as the equivalent for cmp(a, b).)

大意就是cmp()函数已经“离开”了,如果你真的需要cmp()函数,你可以用表达式(a b) - (a b)代替cmp(a,b)

python编程中cmp()函数是什么意思?

cmp( x, y)

Compare the two objects x and y and return an integer according to the outcome. The return value is negative if x y, zero if x == y and strictly positive if x y.

比较2个对象,前者小于后者返回-1,相等则返回0,大于后者返回1.


文章标题:cmp函数python python cmap
本文URL:http://bzwzjz.com/article/dojepgg.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都企业网站建设公司 教育网站设计方案 温江网站设计 高端品牌网站建设 高端网站设计推广 成都网站设计制作公司 网站建设公司 重庆企业网站建设 成都网站建设 重庆手机网站建设 成都网站设计 成都网站设计 企业网站制作 营销网站建设 营销型网站建设 成都网站制作 外贸网站建设 成都网站建设 成都网站制作 成都网站建设 成都网站建设推广 手机网站建设