pythoncmp函數if
㈠ 關於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):
我來解答你的疑問:
如果默認的排序規則不能滿足需求,當然要自己寫了.比如默認的是從小到大排序,你想從打大到小排序,就得自己寫了,在比如,漢字排序,你想用漢字的拼音來排序,也要自己寫.
函數里return之後,這個函數就結束了,之後的語句就不執行了.如果還是不明白,你要看看python基礎里的Python語句執行流程.
排序有很多方法,內部的演算法做了很多優化,就光排序的演算法就可以寫一本書了,裡面的具體細節不用深究.
另外,為什麼返回-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.