国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Python sorted對list和dict排序

瀏覽:3日期:2022-07-22 08:04:48

sorted語法

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

參數(shù)說明:

- iterable -- 可迭代對象。 - key --主要是用來進行比較的元素,只有一個參數(shù),具體的函數(shù)的參數(shù)就是取自于可迭代對象中,指定可迭代對象中的一個元素來進行排序。 - reverse -- 排序規(guī)則,reverse = True 降序 , reverse = False 升序(默認)。

返回: - 一個新list對象

sorted對字典dict排序

①按鍵key排序

from operator import itemgetterdict = {3: ’B’, 1: ’A’, 2: ’C’}# 按key升序 .items()取得3個(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類似lambdad2 = 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 itemgetterdict = {3: ’B’, 1: ’A’, 2: ’C’}# 按value升序 .items()取得3個(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類似lambdad4 = 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 itemgetterdata = [(’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

# 先按照成績降序排序,相同成績的按照名字升序排序: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的混合

先看看我們排序的有哪些類型的數(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}]#### 字典中混合listd1 = {’Li’: [’M’, 7], ’Zhang’: [’E’, 2], ’Wang’: [’P’, 3], ’Du’: [’C’, 2], ’Ma’: [’C’, 9], ’Zhe’: [’H’, 7]}#### 對字典中的多維list進行排序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 itemgetterl1 = [[’Bob’, 95.00, ’A’], [’Alan’, 86.0, ’C’], [’Mandy’, 82.5, ’A’], [’Rob’, 86, ’E’]]# 按先按成績號升序,再按成績數(shù)值升序print(sorted(l1, key=itemgetter(2, 1), reverse=False))# 按先按成績號升序,再按成績數(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# 先按照成績降序排序,相同成績的按照名字升序排序: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前面套一個dict()就可以了print(sorted(d1.items(), key=lambda x:(x[1][1], -ord(x[1][0]) ))) # 對字符比較需要ord。如果是’123’字符串數(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. 對字典中的多維list進行排序

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對list和dict排序的文章就介紹到這了,更多相關(guān)Python sorted對list和dict排序內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 丝袜黄色片 | 国产uv1区二区三区 国产va免费精品高清在线观看 | 免费韩国美女爽快一级毛片 | 亚洲天堂久久久 | 亚洲在线免费 | 久色乳综合思思在线视频 | 日韩中文字幕在线视频 | 日韩成人小视频 | 在线中文字幕亚洲 | 日韩精品中文字幕视频一区 | 欧美日韩综合精品一区二区三区 | 亚洲第一免费播放区 | 亚洲国产欧美在线不卡中文 | 免费一区二区三区 | 韩国黄色一级毛片 | 成人男男黄网色视频免费 | 亚洲精品 欧美 | 国产欧美一区二区精品性色 | 小草青青神马影院 | 精品久久中文网址 | 一区二区三区日韩 | 国产精品一一在线观看 | 波多野结衣福利视频 | 一级毛片一级毛片a毛片欧美 | 国产精品偷伦费观看 | a级毛片毛片免费观看永久 a级毛片毛片免费很很综合 | 男人女人做性全程视视频 | 亚洲欧美在线免费 | 黄色激情网站 | 青青操在线视频 | 中文字幕在线免费观看 | 成人影院欧美大片免费看 | 快色网站 | 成人性生片全套 | 99久久精品免费看国产 | 一本色道久久88亚洲精品综合 | 国产日本三级欧美三级妇三级四 | 国产亚洲精品午夜一区 | 欧美一级在线全免费 | 亚洲精品国产第一区二区三区 | 亚洲日本精品 |