亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python sorted函數(shù)詳解(高級篇)

 更新時間:2018年09月18日 12:01:02   作者:brucewong0516  
本文我們用到了sorted 如何進行按照鍵或者值進行排序,解決了字典的排序問題。文中將進一步詳細介紹sorted的強大。希望對大家有所幫助

sorted 用于對集合進行排序(這里集合是對可迭代對象的一個統(tǒng)稱,他們可以是列表、字典、set、甚至是字符串),它的功能非常強大

1、對列表排序,返回的對象不會改變原列表

list = [1,5,7,2,4]

sorted(list)
Out[87]: [1, 2, 4, 5, 7]
#可以設定時候排序方式,默認從小到大,設定reverse = False 可以從大到小
sorted(list,reverse=False)
Out[88]: [1, 2, 4, 5, 7]

sorted(list,reverse=True)
Out[89]: [7, 5, 4, 2, 1]

2、根據(jù)自定義規(guī)則來排序,使用參數(shù):key

# 使用key,默認搭配lambda函數(shù)使用
sorted(chars,key=lambda x:len(x))
Out[92]: ['a', 'is', 'boy', 'bruce', 'handsome']

sorted(chars,key=lambda x:len(x),reverse= True)
Out[93]: ['handsome', 'bruce', 'boy', 'is', 'a']

3、根據(jù)自定義規(guī)則來排序,對元組構成的列表進行排序

tuple_list = [('A', 1,5), ('B', 3,2), ('C', 2,6)]
#key=lambda x: x[1]中可以任意選定x中可選的位置進行排序
sorted(tuple_list, key=lambda x: x[1]) 

Out[94]: [('A', 1, 5), ('C', 2, 6), ('B', 3, 2)]

sorted(tuple_list, key=lambda x: x[0])
Out[95]: [('A', 1, 5), ('B', 3, 2), ('C', 2, 6)]

sorted(tuple_list, key=lambda x: x[2])
Out[96]: [('B', 3, 2), ('A', 1, 5), ('C', 2, 6)]

4、排序的元素是自定義類

class tuple_list:
 def __init__(self, one, two, three):
  self.one = one
  self.two = two
  self.three = three
 def __repr__(self):
  return repr((self.one, self.two, self.three))


tuple_list_ = [tuple_list('A', 1,5), tuple_list('B', 3,2), tuple_list('C', 2,6)]

sorted(tuple_list_, key=lambda x: x.one)
Out[104]: [('A', 1, 5), ('B', 3, 2), ('C', 2, 6)]

sorted(tuple_list_, key=lambda x: x.two)
Out[105]: [('A', 1, 5), ('C', 2, 6), ('B', 3, 2)]

sorted(tuple_list_, key=lambda x: x.three)
Out[106]: [('B', 3, 2), ('A', 1, 5), ('C', 2, 6)]

5、sorted 也可以根據(jù)多個字段來排序

class tuple_list:
 def __init__(self, one, two, three):
  self.one = one
  self.two = two
  self.three = three
 def __repr__(self):
  return repr((self.one, self.two, self.three))

tuple_list_ = [tuple_list('C', 1,5), tuple_list('A', 3,2), tuple_list('C', 2,6)]
# 首先根據(jù)one的位置來排序,然后根據(jù)two的位置來排序
sorted(tuple_list_, key=lambda x:(x.one, x.two))
Out[112]: [('A', 3, 2), ('C', 1, 5), ('C', 2, 6)]

6、使用operator 中的itemgetter方法和attrgetter方法

tuple_list = [('A', 1,5), ('B', 3,2), ('C', 2,6)]

class tuple_list_class:
 def __init__(self, one, two, three):
  self.one = one
  self.two = two
  self.three = three
 def __repr__(self):
  return repr((self.one, self.two, self.three))

tuple_list_ = [tuple_list_class('C', 1,5), tuple_list_class('A', 3,2), tuple_list_class('C', 2,6)]

from operator import itemgetter
sorted(tuple_list, key=itemgetter(1))

Out[119]: [('A', 1, 5), ('C', 2, 6), ('B', 3, 2)]

from operator import attrgetter
sorted(tuple_list_, key=attrgetter('one')) # attrgetter 傳入的參數(shù)必須是str

Out[120]: [('A', 3, 2), ('C', 1, 5), ('C', 2, 6)]

# 如果是根據(jù)多個類的參數(shù)排序,按照參數(shù)定義順序
from operator import attrgetter
sorted(tuple_list_, key=attrgetter('two','one'))

Out[121]: [('C', 1, 5), ('C', 2, 6), ('A', 3, 2)]

高級用法

有時候,我們要處理的數(shù)據(jù)內(nèi)的元素不是一維的,而是二維的甚至是多維的,那要怎么進行排序呢?這時候,sorted()函數(shù)內(nèi)的key參數(shù)就派上用場了!從幫助信息上可以了解到,key參數(shù)可傳入一個自定義函數(shù)。那么,該如何使用呢?讓我們看看如下代碼:

>>>l=[('a', 1), ('b', 2), ('c', 6), ('d', 4), ('e', 3)]
>>>sorted(l, key=lambda x:x[0])
Out[39]: [('a', 1), ('b', 2), ('c', 6), ('d', 4), ('e', 3)]
>>>sorted(l, key=lambda x:x[0], reverse=True)
Out[40]: [('e', 3), ('d', 4), ('c', 6), ('b', 2), ('a', 1)]
>>>sorted(l, key=lambda x:x[1])
Out[41]: [('a', 1), ('b', 2), ('e', 3), ('d', 4), ('c', 6)]
>>>sorted(l, key=lambda x:x[1], reverse=True)
Out[42]: [('c', 6), ('d', 4), ('e', 3), ('b', 2), ('a', 1)]

這里,列表里面的每一個元素都為二維元組,key參數(shù)傳入了一個lambda函數(shù)表達式,其x就代表列表里的每一個元素,然后分別利用索引返回元素內(nèi)的第一個和第二個元素,這就代表了sorted()函數(shù)利用哪一個元素進行排列。而reverse參數(shù)就如同上面講的一樣,起到逆排的作用。默認情況下,reverse參數(shù)為False。
當然,正如一開始講到的那樣,如果想要對列表直接進行排序操作,可以用成員方法sort()來做:

>>>l.sort(key=lambda x : x[1])
>>>l
Out[45]: [('a', 1), ('b', 2), ('e', 3), ('d', 4), ('c', 6)]
>>>l.sort(key=lambda x : x[1], reverse=True)
>>>l
Out[47]: [('c', 6), ('d', 4), ('e', 3), ('b', 2), ('a', 1)]

對于三維及以上的數(shù)據(jù)排排序,上述方法同樣適用。

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接

相關文章

  • python 5個實用的技巧

    python 5個實用的技巧

    這篇文章主要介紹了python 5個實用的技巧,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-09-09
  • 教你怎么用Python實現(xiàn)自動生日祝福

    教你怎么用Python實現(xiàn)自動生日祝福

    這篇文章主要介紹了教你怎么用Python實現(xiàn)自動生日祝福,文中有非常詳細的代碼示例,對正在學習python的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Numpy中的mask的使用

    Numpy中的mask的使用

    這篇文章主要介紹了Numpy中的mask的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 解決python xx.py文件點擊完之后一閃而過的問題

    解決python xx.py文件點擊完之后一閃而過的問題

    今天小編就為大家分享一篇解決python xx.py文件點擊完之后一閃而過的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python實現(xiàn)windows下文件備份腳本

    python實現(xiàn)windows下文件備份腳本

    這篇文章主要為大家詳細介紹了python實現(xiàn)windows下文件備份的腳本,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Keras自動下載的數(shù)據(jù)集/模型存放位置介紹

    Keras自動下載的數(shù)據(jù)集/模型存放位置介紹

    這篇文章主要介紹了Keras自動下載的數(shù)據(jù)集/模型存放位置介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Django實現(xiàn)網(wǎng)頁分頁功能

    Django實現(xiàn)網(wǎng)頁分頁功能

    這篇文章主要介紹了Django實現(xiàn)網(wǎng)頁分頁功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • opencv-python 讀取圖像并轉換顏色空間實例

    opencv-python 讀取圖像并轉換顏色空間實例

    今天小編就為大家分享一篇opencv-python 讀取圖像并轉換顏色空間實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python處理mat文件的三種方式小結

    Python處理mat文件的三種方式小結

    這篇文章主要介紹了Python處理mat文件的三種方式小結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python opencv鼠標事件實現(xiàn)畫框圈定目標獲取坐標信息

    python opencv鼠標事件實現(xiàn)畫框圈定目標獲取坐標信息

    這篇文章主要為大家詳細介紹了python opencv鼠標事件實現(xiàn)畫框圈定目標,獲取坐標信息,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08

最新評論