cmp( x, y)
在綏芬河等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作定制設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,全網(wǎng)整合營銷推廣,成都外貿(mào)網(wǎng)站建設(shè),綏芬河網(wǎng)站建設(shè)費(fèi)用合理。
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個(gè)對(duì)象,前者小于后者返回-1,相等則返回0,大于后者返回1.
cmp是python的內(nèi)建函數(shù).
cmp(x,y) 用于 compare x 和 y的值.
sort(cmp)只是用于說明,python中函數(shù)也是可以作為參數(shù)傳入其他函數(shù)來進(jìn)行調(diào)用的,排序的依據(jù)就是cmp.
3開始沒這個(gè)函數(shù)了,官方文檔是這么寫的
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()函數(shù)已經(jīng)“離開”了,如果你真的需要cmp()函數(shù),你可以用表達(dá)式(a b) - (a b)代替cmp(a,b)
numbers.sort這種用法是錯(cuò)誤的,如果你想要排序,則用如下語句:
num_sort=sorted(numbers,key=None,reverse=False)
新的list num_sort才是一個(gè)排序后的列表。然后,你自定義的cmp過程只能對(duì)比兩個(gè)數(shù)字,而能對(duì)比列表中的各個(gè)元素,python3解釋器不知道你要做什么,所以才會(huì)出錯(cuò)。