python - 通过使用子字典中键的值对包含子字典的字典进行排序
问题描述
假设我有一个包含字典的字典,如下例所示:
a_dict = {
'abcd': {'name': 'John', 'height': 180},
'asdf': {'name': 'Lex', 'height': 160},
'csxf': {'name': 'Amber', 'height': 193},
}
如何使用子字典中键的值对字典进行排序,即:'height'
键的值?
解决方案
你可以试试这样的
dict(sorted(a_dict.items(), key=lambda item: item[1]["height"]))
推荐阅读
- jwt - 更改 JWT 密码时如何防止注销用户?
- terraform - 在 Rundeck 中运行 Terraform 进程
- javascript - 在选择选项中更改字符串
- entity-framework - .Net Core EF 迁移成功,但我找不到数据库
- r - 更改闪亮应用的直方图颜色
- postgresql - 当我已经遵循格式时,为什么我不能删除 Postgresql-Pgadmin 中的一行?
- installation - 在 laravel 5.4 中安装 guzzle
- sql - 为 SQL Server 中的列中的每个值计算冗余
- python - 由 Pyinstaller 创建的 .exe 在执行乘法矩阵时关闭
- python-3.x - 我在 python 密码学中不断收到 Invalid Token 错误(cryptography.fernet.InvalidToken)