【Python】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]]
}

注意: 要想学会使用sorted复杂的list和混合dict进行排序,最好先弄懂sorted如何对list和dict进行排序的,key= lambda 或itemgetter是如何对可迭代的list和dict进行元组化和比较的

1. 二维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'), reverse=True))

[{‘name’: ‘alice’, ‘score’: 38}, {‘name’: ‘christ’, ‘score’: 28}, {‘name’: ‘darl’, ‘score’: 28}, {‘name’: ‘bob’, ‘score’: 18}]
[{‘name’: ‘alice’, ‘score’: 38}, {‘name’: ‘darl’, ‘score’: 28}, {‘name’: ‘christ’, ‘score’: 28}, {‘name’: ‘bob’, ‘score’: 18}]

3. 字典中混合list
d1 = {'Li': ['M', 7], 'Zhang': ['E', 2], 'Wang': ['P', 3], 'Du': ['C', 2], 'Ma': ['C', 9], 'Zhe': ['H', 7]}
# 按value(list)里的数组升序,再按字母降序
print(sorted(d1.items(), key=lambda x:(x[1][1], -ord(x[1][0]) )))  # 对字符比较需要ord。如果是'123'字符串数字可以使用int。
# sort返回的是list,如果需要转为dict,再sorted前面套一个dict()就可以了
print(dict(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])]
{‘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]]}

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值