欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

sorted(list)
Out[87]: [1, 2, 4, 5, 7]
#可以設(shè)定時候排序方式,默認(rèn)從小到大,設(shè)定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,默認(rèn)搭配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ī)則來排序,對元組構(gòu)成的列表進行排序

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ù)表達(dá)式,其x就代表列表里的每一個元素,然后分別利用索引返回元素內(nèi)的第一個和第二個元素,這就代表了sorted()函數(shù)利用哪一個元素進行排列。而reverse參數(shù)就如同上面講的一樣,起到逆排的作用。默認(rèn)情況下,reverse參數(shù)為False。
當(dāng)然,正如一開始講到的那樣,如果想要對列表直接進行排序操作,可以用成員方法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ù)排排序,上述方法同樣適用。

總結(jié)

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

相關(guān)文章

最新評論