按字典值对字典列表进行排序 [英] Sorting a list of dicts by dict values

查看:77
本文介绍了按字典值对字典列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下词典列表

a = [{23:100}, {3:103}, {2:102}, {36:103}, {43:123}]

如何对其进行排序以获取:

How can I sort it to get:

a = [{43:123}, {3:103}, {36:103}, {2:102}, {23:100}]

我的意思是按照列表的dict值对列表进行降序排序.

I mean, to sort the list by its dicts' values, in descending order.

推荐答案

除了brandizzi的答案,您还可以选择:

In addition to brandizzi's answer, you could go with:

sorted(a, key=dict.values, reverse=True)

差不多,但可能更惯用.

Pretty much the same thing, but possibly more idiomatic.

这篇关于按字典值对字典列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆