Python sort方法

來源:互聯網
上載者:User

標籤:pts   been   change   app   word   pytho   loss   desc   err   

官方文檔:

sort(*key=Nonereverse=False)

This method sorts the list in place, using only < comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state).

sort() accepts two arguments that can only be passed by keyword (keyword-only arguments):

key specifies a function of one argument that is used to extract a comparison key from each list element (for example, key=str.lower). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of None means that list items are sorted directly without calculating a separate key value.

The functools.cmp_to_key() utility is available to convert a 2.x style cmp function to a key function.

reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

This method modifies the sequence in place for economy of space when sorting a large sequence. To remind users that it operates by side effect, it does not return the sorted sequence (use sorted() to explicitly request a new sorted list instance).

The sort() method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).

CPython implementation detail: While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python makes the list appear empty for the duration, and raises ValueError if it can detect that the list has been mutated during a sort.

 

 

sort()只能對純數字或字母進行排序, 否則報錯:

>>> alist[1, 3, 4, 7, 8, 9]>>> alist.append(‘a‘)>>> alist.sort()Traceback (most recent call last):  File "<pyshell#83>", line 1, in <module>    alist.sort()TypeError: ‘<‘ not supported between instances of ‘str‘ and ‘int‘>>> 

 

sort()可以不帶參數或最多帶兩個關鍵字參數,預設key=Nonereverse=False, 預設升序排列:

>>> alist = [1,4,2,7,9,3]>>> alist.sort()>>> alist[1, 2, 3, 4, 7, 9]>>> 

 

指定reverse=True時降序排列:

>>> alist = [1,4,2,7,9,3]>>> alist.sort(reverse=True)>>> alist[9, 7, 4, 3, 2, 1]>>> 

 

如果想保持原list不變,拷貝一個新list出來排序:

>>> alist = [1,4,2,7,9,3]>>> blist = alist[:] #不能寫blist = alist,這樣寫是讓blist指向alist同一記憶體空間>>> blist.sort()>>> alist[1, 4, 2, 7, 9, 3]>>> blist[1, 2, 3, 4, 7, 9]>>> 

 

或者用sorted()方法:

>>> alist = [1,4,2,7,9,3]>>> blist = sorted(alist)>>> alist[1, 4, 2, 7, 9, 3]>>> blist[1, 2, 3, 4, 7, 9]>>> 

 

Python sort方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.