Python sorted對(duì)list和dict排序
sorted語(yǔ)法
sorted(iterable, key=None, reverse=False)
參數(shù)說(shuō)明:
- iterable -- 可迭代對(duì)象。
- key --主要是用來(lái)進(jìn)行比較的元素,只有一個(gè)參數(shù),具體的函數(shù)的參數(shù)就是取自于可迭代對(duì)象中,指定可迭代對(duì)象中的一個(gè)元素來(lái)進(jìn)行排序。
- reverse -- 排序規(guī)則,reverse = True 降序 , reverse = False 升序(默認(rèn))。
返回:
- 一個(gè)新list對(duì)象
sorted對(duì)字典dict排序
①按鍵key排序
from operator import itemgetter dict = {3: 'B', 1: 'A', 2: 'C'} # 按key升序 .items()取得3個(gè)(key,value) # lambda x: x[0]取(key,value)的key 即(3,1,2) d1 = sorted(dict.items(), key=lambda x: x[0], reverse=False) # <class 'list'> # 按key降序 itemgetter類(lèi)似lambda d2 = sorted(dict.items(), key=itemgetter(0), reverse=True) # <class 'list'> # 輸出 print(d1, type(d1)) # [(1, 'A'), (2, 'C'), (3, 'B')] <class 'list'> print(d2, type(d2)) # [(3, 'B'), (2, 'C'), (1, 'A')] <class 'list'>
[(1, ‘A'), (2, ‘C'), (3, ‘B')] <class ‘list'>
[(3, ‘B'), (2, ‘C'), (1, ‘A')] <class ‘list'>
②按值value排序
from operator import itemgetter dict = {3: 'B', 1: 'A', 2: 'C'} # 按value升序 .items()取得3個(gè)(key,value) # lambda x: x[1]取(key,value)的value 即('B','A','C') d3 = sorted(dict.items(), key=lambda x: x[1], reverse=False) # <class 'list'> # 按value降序 itemgetter類(lèi)似lambda d4 = sorted(dict.items(), key=itemgetter(1), reverse=True) # <class 'list'> print(d3, type(d3)) # [(1, 'A'), (3, 'B'), (2, 'C')] <class 'list'> print(d4, type(d4)) # [(2, 'C'), (3, 'B'), (1, 'A')] <class 'list'>
[(1, ‘A'), (3, ‘B'), (2, ‘C')] <class ‘list'>
[(2, ‘C'), (3, ‘B'), (1, ‘A')] <class ‘list'>
sorted排序list
①按一種規(guī)則排序list
from operator import itemgetter data = [('c', 3, 'Apple'), ('d', 1, 'Cat'), ('a', 2, 'Banana')] # 根據(jù)字母升序 print(sorted(data, key=lambda x: x[0], reverse=False)) # <class 'list'> # 根據(jù)數(shù)字升序 print(sorted(data, key=lambda x: x[1], reverse=False)) # <class 'list'> # 根據(jù)單詞升序 print(sorted(data, key=lambda x: x[2], reverse=False)) # <class 'list'>
[('a', 2, 'Banana'), ('c', 3, 'Apple'), ('d', 1, 'Cat')]
[('d', 1, 'Cat'), ('a', 2, 'Banana'), ('c', 3, 'Apple')]
[('c', 3, 'Apple'), ('a', 2, 'Banana'), ('d', 1, 'Cat')]
②按多種規(guī)則排序list
# 先按照成績(jī)降序排序,相同成績(jī)的按照名字升序排序: d1 = [{'name':'alice', 'score':38}, {'name':'bob', 'score':18}, {'name':'darl', 'score':28}, {'name':'christ', 'score':28}] l = sorted(d1, key=lambda x:(-x['score'], x['name'])) print(l)
[{'name': 'alice', 'score': 38}, {'name': 'christ', 'score': 28}, {'name': 'darl', 'score': 28}, {'name': 'bob', 'score': 18}]
sorted排序list和dict的混合
先看看我們排序的有哪些類(lèi)型的數(shù)據(jù)結(jié)構(gòu)
#### 二維list排序 l1 = [['Bob', 95.00, 'A'], ['Alan', 86.0, 'C'], ['Mandy', 82.5, 'A'], ['Rob', 86, 'E']] #### list中混合字典 l2 = [{'name':'alice', 'score':38}, {'name':'bob', 'score':18}, {'name':'darl', 'score':28}, {'name':'christ', 'score':28}] #### 字典中混合list d1 = {'Li': ['M', 7], 'Zhang': ['E', 2], 'Wang': ['P', 3], 'Du': ['C', 2], 'Ma': ['C', 9], 'Zhe': ['H', 7]} #### 對(duì)字典中的多維list進(jìn)行排序 d2 = { 'Apple': [['44', 88], ['11', 33], ['22', 88]], 'Banana': [['55', 43], ['11', 68], ['44', 22]], 'Orange':[['22', 22], ['55', 41], ['44', 42], ['33', 22]] }
二維list排序
from operator import itemgetter l1 = [['Bob', 95.00, 'A'], ['Alan', 86.0, 'C'], ['Mandy', 82.5, 'A'], ['Rob', 86, 'E']] # 按先按成績(jī)號(hào)升序,再按成績(jī)數(shù)值升序 print(sorted(l1, key=itemgetter(2, 1), reverse=False)) # 按先按成績(jī)號(hào)升序,再按成績(jī)數(shù)值降序序 print(sorted(l1, key=lambda x:(x[2], -x[1]), reverse=False))
[[‘Mandy', 82.5, ‘A'], [‘Bob', 95.0, ‘A'], [‘Alan', 86.0, ‘C'], [‘Rob', 86, ‘E']]
[[‘Bob', 95.0, ‘A'], [‘Mandy', 82.5, ‘A'], [‘Alan', 86.0, ‘C'], [‘Rob', 86, ‘E']]
2. list中混合字典
from operator import itemgetter # 先按照成績(jī)降序排序,相同成績(jī)的按照名字升序排序: l2 = [{'name':'alice', 'score':38}, {'name':'bob', 'score':18}, {'name':'darl', 'score':28}, {'name':'christ', 'score':28}] print(sorted(l2, key=lambda x:(-x['score'], x['name']))) print(sorted(l2, key=itemgetter('score', 'name')))
[{‘name': ‘a(chǎn)lice', ‘score': 38}, {‘name': ‘christ', ‘score': 28}, {‘name': ‘darl', ‘score': 28}, {‘name': ‘bob', ‘score': 18}]
[{‘name': ‘bob', ‘score': 18}, {‘name': ‘christ', ‘score': 28}, {‘name': ‘darl', ‘score': 28}, {‘name': ‘a(chǎn)lice', ‘score': 38}]
3. 字典中混合list
d1 = {'Li': ['M', 7], 'Zhang': ['E', 2], 'Wang': ['P', 3], 'Du': ['C', 2], 'Ma': ['C', 9], 'Zhe': ['H', 7]} # sort返回的是list,如果需要轉(zhuǎn)為dict,再sorted前面套一個(gè)dict()就可以了 print(sorted(d1.items(), key=lambda x:(x[1][1], -ord(x[1][0]) ))) # 對(duì)字符比較需要ord。如果是'123'字符串?dāng)?shù)字可以使用int。 # print(sorted(d1.items(), key=lambda x:(x[1][1], -ord(x[1][0]) )))
[(‘Zhang', [‘E', 2]), (‘Du', [‘C', 2]), (‘Wang', [‘P', 3]), (‘Li', [‘M', 7]), (‘Zhe', [‘H', 7]), (‘Ma', [‘C', 9])]
4. 對(duì)字典中的多維list進(jìn)行排序
d2 = { 'Apple': [['44', 88], ['11', 33], ['22', 88]], 'Banana': [['55', 43], ['11', 68], ['44', 22]], 'Orange':[['22', 22], ['55', 41], ['44', 42], ['33', 22]] } for key, value in d2.items(): d2[key] = sorted(value, key=lambda x:(x[1], -int(x[0]))) # 按list第二列升序,相同則按第一列降序,參考二維list排序 print(d2)
{‘Apple': [[‘11', 33], [‘44', 88], [‘22', 88]], ‘Banana': [[‘44', 22], [‘55', 43], [‘11', 68]], ‘Orange': [[‘33', 22], [‘22', 22], [‘52', 41], [‘44', 42]]}
到此這篇關(guān)于Python sorted對(duì)list和dict排序的文章就介紹到這了,更多相關(guān)Python sorted對(duì)list和dict排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python中字典dict排序sorted的實(shí)現(xiàn)
- Python OrderedDict字典排序方法詳解
- Python按照l(shuí)ist dict key進(jìn)行排序過(guò)程解析
- Python 按字典dict的鍵排序,并取出相應(yīng)的鍵值放于list中的實(shí)例
- python 對(duì)key為時(shí)間的dict排序方法
- python中dict字典的查詢(xún)鍵值對(duì) 遍歷 排序 創(chuàng)建 訪(fǎng)問(wèn) 更新 刪除基礎(chǔ)操作方法
- python 字典(dict)按鍵和值排序
- Python中字典(dict)和列表(list)的排序方法實(shí)例
- Python中dict排序的兩種方法
相關(guān)文章
Django?項(xiàng)目配置拆分獨(dú)立的實(shí)現(xiàn)
Django 項(xiàng)目中,我們默認(rèn)的配置是都在 settings.py 文件里面的,但是實(shí)際本地調(diào)試和線(xiàn)上應(yīng)該是需要兩個(gè)環(huán)境的,我們現(xiàn)在來(lái)拆分下配置,本文就詳細(xì)的來(lái)介紹一下2021-11-11keras實(shí)現(xiàn)VGG16 CIFAR10數(shù)據(jù)集方式
這篇文章主要介紹了keras實(shí)現(xiàn)VGG16 CIFAR10數(shù)據(jù)集方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Python利用代碼計(jì)算2個(gè)坐標(biāo)之間的距離
這篇文章主要介紹了Python利用代碼計(jì)算2個(gè)坐標(biāo)之間的距離,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08python正則表達(dá)式函數(shù)match()和search()的區(qū)別
match()和search()都是python中的正則匹配函數(shù),那這兩個(gè)函數(shù)有何區(qū)別呢?本文詳細(xì)介紹了這2個(gè)函數(shù)的區(qū)別2021-10-10Python使用matplotlib繪圖無(wú)法顯示中文問(wèn)題的解決方法
這篇文章主要介紹了Python使用matplotlib繪圖無(wú)法顯示中文問(wèn)題的解決方法,結(jié)合具體實(shí)例形式分析了Python使用matplotlib繪圖時(shí)出現(xiàn)中文亂碼的原因與相關(guān)解決方法,需要的朋友可以參考下2018-03-03