列表嵌套字典,根据字典某一key排序
2021-07-01 02:07
标签:image oss sort out ESS 嵌套 处理 vpd 根据 如果排序字段是数字类型,如果我们需要对该字段进行逆序排,也可以这么写: 列表嵌套字典,根据字典某一key排序 标签:image oss sort out ESS 嵌套 处理 vpd 根据 原文地址:http://blog.51cto.com/10412806/2174512
按照字典中某一个key排序
In [87]: a = [{"name": "牛郎", "age": 23},{"name":"许仙", "age": 20},{"name":"董永", "age": 18}]
In [88]: a
Out[88]:
[{‘name‘: ‘牛郎‘, ‘age‘: 23},
{‘name‘: ‘许仙‘, ‘age‘: 20},
{‘name‘: ‘董永‘, ‘age‘: 18}]
In [89]: a.sort(key=lambda x:(x[‘name‘], x[‘age‘])) # 根据字典中name,age排序
In [90]: a
Out[90]:
[{‘name‘: ‘牛郎‘, ‘age‘: 23},
{‘name‘: ‘董永‘, ‘age‘: 18},
{‘name‘: ‘许仙‘, ‘age‘: 20}]
In [91]: a.sort(key=lambda x:(x[‘age‘], x[‘name‘]))
In [92]: a
Out[92]:
[{‘name‘: ‘董永‘, ‘age‘: 18},
{‘name‘: ‘许仙‘, ‘age‘: 20},
{‘name‘: ‘牛郎‘, ‘age‘: 23}]
a.sort(key=lambd x :(x[‘name‘], -x[‘age‘]))
下一篇:Python_基础个人理解_闭包
文章标题:列表嵌套字典,根据字典某一key排序
文章链接:http://soscw.com/index.php/essay/100121.html