㈠ 关于python的覆盖__cmp__的两点问题

__cmp__
对 int、str 等内置数据类型排序时,Python的 sorted() 按照默认的比较函数 cmp 排序,但是,如果对一组 Student 类的实例排序时,就必须提供我们自己的特殊方法 __cmp__():

class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
def __str__(self):
return '(%s: %s)' % (self.name, self.score)
__repr__ = __str__

def __cmp__(self, s):
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
else:
return 0
上述 Student 类实现了__cmp__()方法,__cmp__用实例自身self和传入的实例 s 进行比较,如果 self 应该排在前面,就返回 -1,如果 s 应该排在前面,就返回1,如果两者相当,返回 0。

Student类实现了按name进行排序:

>>> L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 77)]
>>> print sorted(L)
[(Alice: 77), (Bob: 88), (Tim: 99)]
注意: 如果list不仅仅包含 Student 类,则 __cmp__ 可能会报错:

L = [Student('Tim', 99), Student('Bob', 88), 100, 'Hello']
print sorted(L)
请思考如何解决。

class Student(object):

def __init__(self, name, score):
self.name = name
self.score = score

def __str__(self):
return '(%s: %s)' % (self.name, self.score)

__repr__ = __str__

def __cmp__(self, s):
if(self.score<s.score):
return 1
if(self.score>s.score):
return -1
if(self.score==s.score):
if(self.name>s.name):
return 1;
if(self.name<s.name):
return -1
return 0

L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)]
print sorted(L)

㈡ python 排序 def reversed_cmp(x, y):

我来解答你的疑问:

  1. 如果默认的排序规则不能满足需求,当然要自己写了.比如默认的是从小到大排序,你想从打大到小排序,就得自己写了,在比如,汉字排序,你想用汉字的拼音来排序,也要自己写.

  2. 函数里return之后,这个函数就结束了,之后的语句就不执行了.如果还是不明白,你要看看python基础里的Python语句执行流程.

  3. 排序有很多方法,内部的算法做了很多优化,就光排序的算法就可以写一本书了,里面的具体细节不用深究.

  4. 另外,为什么返回-1的问题. 不知道你有没有其他编程语言的背景,知不知道什么是重载.如果自己写排序规则,第二个参数是一个函数.

你完全没搞懂他的意思,而且你的python基础太差了.

㈢ python大神,大神进,关于对象,cmp规则,工厂函数问题

1、列表对象是list类的实例化对象,list类的基类是object

>>> isinstance(l,list)

True

>>> issubclass(list,object)

True

2、cmp的跨类型比较,这里我找到的一段文字

html">1.对两个列表的元素进行比较.
2.如果比较的元素是同类型的,则比较其值,返回结果.
3.如果两个元素不是同一种类型,则检查它们是否是数字.
a.如果是数字,执行必要的数字强制类型转换,然后比较.
b.如果有一方的元素是数字,则另一方的元素"大"(数字是"最小的")
c.否则,通过类型名字的字母顺序进行比较.
4.如果有一个列表首先到达末尾,则另一个长一点的列表"大".
5.如果我们用尽了两个列表的元素而且所有元素都是相等的,那么结果就是个平局,就
是说返回一个0.

3、关于工厂函数的定义,可以看看这里:http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch23.html#objects.creation.factory

最好参照实例自己做一做,比较容易弄懂

4、列出所有内置函数的方法我也不知道,感觉也太多了。

㈣ 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=',

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)

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

numbers.sort这种用法是错误的,如果你想要排序,则用如下语句:
num_sort=sorted(numbers,key=None,reverse=False)
新的list num_sort才是一个排序后的列表。然后,你自定义的cmp过程只能对比两个数字,而能对比列表中的各个元素,python3解释器不知道你要做什么,所以才会出错。

㈦ python3没有了Cmp函数,自定义的排序sort方法不是需要它作参数吗!!怎么办

自定义排序用key关键字
>>> a=['abc','abcd','ab']
>>> a.sort(key=len) #使用len函数返回的大小排序
>>> a
['ab', 'abc', 'abcd']

key和reverse是有的,试一下就知道了

㈧ 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.