资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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
文章源于:http://cdkjz.cn/article/dojepgg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220