天天看點

Python内置函數 — sort,sorted

1、sort

清單的屬性方法,對清單進行排序,預設升序,傳回None值。

源碼注釋:

"""
Sort the list in ascending order and return None.

The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
order of two equal elements is maintained).

If a key function is given, apply it once to each list item and sort them,
ascending or descending, according to their function values.

The reverse flag can be set to sort in descending order.
"""
      

說明:

按升序排序清單并傳回None。

排序到位(即清單本身被修改)且穩定(即保持兩個相等元素的順序)。

如果給定了鍵函數,則将其應用于每個清單項一次,并根據其函數值對其進行升序或降序排序。

反轉标志可以設定為按降序排序。

文法結構:

list.sort(key=None, reverse=False)

參數:

  • key,可選參數,預設值為None。如果key有值,則按照key的函數規則對清單進行排序,可以用來自定義排序規則。
  • reverse,可選參數,排序規則,reverse = True 降序 , reverse = False 升序(預設)

傳回值:

  • 傳回 None值,原清單的元素排序發生改變。

用法執行個體:

(1)升序排序(預設升序)

lst = [1, 4, 5, 3, 2]
lst.sort()
print(lst)
-------------------------------------------------------------------------------------
運作結果:
[1, 2, 3, 4, 5]
           

(2)降序排序

lst = [1, 3, 4, 2, 5]
lst.sort(reverse=True)
print(lst)
---------------------------------------------------------------------------------
運作結果:
[5, 4, 3, 2, 1]
           

(3)使用key自定義排序

lst = [[1, 2], [3, 4], [4, 1], [2, 3]]
lst.sort()
print(lst)
------------------------------------------------------------------------------------
運作結果:
[[1, 2], [2, 3], [3, 4], [4, 1]]
           

說明:預設是根據二維清單的第一個元素升序排序,下面我們來使用key這個參數來實作根據二維清單的第二個元素排序。

lst = [[1, 2], [3, 4], [4, 1], [2, 3]]
lst.sort(key=lambda x: x[1])
print(lst)
-----------------------------------------------------------------------------------
運作結果:
[[4, 1], [1, 2], [2, 3], [3, 4]]
           

2、sorted

Python内置函數,對可疊代對象進行排序,預設升序,傳回一個新的清單。

源碼注釋:

"""
Return a new list containing all items from the iterable in ascending order.

A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
"""      

說明:

傳回一個新清單,其中按升序包含可疊代項中的所有項。

可以提供自定義鍵函數來自定義排序順序,并且可以設定反轉标志以請求按降序排序的結果。

文法格式:

sorted(iterable, key=None, reverse=False)

參數:

  • iterable,可疊代對象
  • key,可選參數,預設值為None。如果key有值,則按照key的函數規則對清單進行排序,可以用來自定義排序規則。
  • reverse,可選參數,排序規則,reverse = True 降序 , reverse = False 升序(預設)

傳回值:

  • 傳回一個新的清單

用法執行個體:

(1)升序排序(預設排序)

a = [1, 4, 5, 3, 2]
b = sorted(a)
print('a=', a)
print('b=', b)
------------------------------------------------------------------------------------
運作結果:
a= [1, 4, 5, 3, 2]
b= [1, 2, 3, 4, 5]
           

sorted函數沒有改變原清單a,按照升序排序生成了一個新的清單b 。

(2)降序排序

a = [1, 4, 5, 3, 2]
b = sorted(a, reverse=True)
print('a=', a)
print('b=', b)
---------------------------------------------------------------------------------
運作結果:
a= [1, 4, 5, 3, 2]
b= [5, 4, 3, 2, 1]
           

(3)使用key自定義排序

lst = [('b', 2), ('a', 1), ('d', 4), ('c', 3)]
lst_new = sorted(lst, key=lambda x: x[1], reverse=True)
print('lst=', lst)
print('lst_new=', lst_new)
-------------------------------------------------------------------------------------
運作結果:
lst= [('b', 2), ('a', 1), ('d', 4), ('c', 3)]
lst_new= [('d', 4), ('c', 3), ('b', 2), ('a', 1)]
           

(4)将清單中的元素按照絕對值大小排序

lst = [-5, -2, 1, 3, 4]
print(sorted(lst, key=lambda x: abs(x)))
---------------------------------------------------------------------------------------
運作結果:
[1, -2, 3, 4, -5]
           

3、sort和sorted的差別

  • sort函數是清單的屬性方法,sorted是Python的内置函數。
  • sort函數是對原清單的元素進行排序,sorted函數是對可疊代對象的元素進行排序,生成一個新清單。

reference:

https://docs.python.org/zh-cn/3.8/library/functions.html#sorted

python中sort()函數的key參數用法__小張要敲代碼的部落格-CSDN部落格_sort函數python key

Python3 sorted() 函數 | 菜鳥教程