python - Python 整理出一个复杂的字典
问题描述
我列了一个清单
data = [{"type":"house", "cost":500, "extra":False, "order":1},
{"type":"condo", "cost":40, "extra":False, "order":4},
{"type":"house", "cost":120, "extra":True, "order":2},
{"type":"house", "cost":800, "extra":True, "order":3}]
order
并希望按键对数据进行排序
你会怎么解决?
解决方案
您不是在对字典进行排序,而是在对字典列表进行排序。
尝试这个:
data = [{"type":"house", "cost":500, "extra":False, "order":1},
{"type":"condo", "cost":40, "extra":False, "order":4},
{"type":"house", "cost":120, "extra":True, "order":2},
{"type":"house", "cost":800, "extra":True, "order":3}]
length = len (data)
for index1 in range (length) :
for index2 in range (length) :
if data [index2]['order'] > data [index1]['order'] :
data [index2], data [index1] = data [index1], data [index2]
print (data)
推荐阅读
- python - 将类设置为变量时出现“名称未定义”
- sql - 需要拉动客户及其各种销售活动——仅在特定活动类型发生的情况下
- laravel - Laravel - 8.* 图片库
- google-sheets - 查找最小数值并返回不同行中的单元格
- python - 如何在 Python 中解决时间问题
- sql - SQL - 如何获得大于不同列中当前值的 3 个前导值的总和
- keycloak - Keycloak 联合用户 - 在自定义 SPI 中触发密码重置电子邮件
- python - 创建具有权重和限制的随机数列表
- c++ - 为什么 std::vector 初始化它的内容?
- python - 在张量流中预训练 GAN NCHW 到 NHWC