欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python sorted对list和dict排序

程序员文章站 2022-03-03 21:28:19
sorted语法sorted(iterable, key=none, reverse=false)参数说明:- iterable -- 可迭代对象。 - key --主要是用来进行比较的元素,只有一个...

sorted语法

sorted(iterable, key=none, reverse=false)

参数说明:

 - iterable -- 可迭代对象。
 - key --主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
 - reverse -- 排序规则,reverse = true 降序 , reverse = false 升序(默认)。

返回:
 - 一个新list对象 

sorted对字典dict排序

①按键key排序

from operator import itemgetter
dict = {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类似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个(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类似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

①按一种规则排序list

from operator import itemgetter
data = [('c', 3, 'apple'), ('d', 1, 'cat'), ('a', 2, 'banana')]
# 根据字母升序
print(sorted(data, key=lambda x: x[0], reverse=false)) # <class 'list'>
# 根据数字升序
print(sorted(data, key=lambda x: x[1], reverse=false)) # <class 'list'>
# 根据单词升序
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')]

②按多种规则排序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的混合

 先看看我们排序的有哪些类型的数据结构

#### 二维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]}

#### 对字典中的多维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 itemgetter
l1 = [['bob', 95.00, 'a'], ['alan', 86.0, 'c'], ['mandy', 82.5, 'a'], ['rob', 86, 'e']]
# 按先按成绩号升序,再按成绩数值升序
print(sorted(l1, key=itemgetter(2, 1), reverse=false))
# 按先按成绩号升序,再按成绩数值降序序
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': ‘alice', ‘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': ‘alice', ‘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,如果需要转为dict,再sorted前面套一个dict()就可以了
print(sorted(d1.items(), key=lambda x:(x[1][1], -ord(x[1][0]) ))) # 对字符比较需要ord。如果是'123'字符串数字可以使用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]]}

到此这篇关于python sorted对list和dict排序的文章就介绍到这了,更多相关python sorted对list和dict排序内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!