【发布时间】:2019-04-11 02:16:13
【问题描述】:
我有一个字典,其中包含一个字典列表,它又包含列表。该字典包含有关公寓房间类型中是否存在某些特征(布尔值)的信息。我想提取与其中一个房间相关的字典 - 例如'卧室'。
我尝试了几种切片方法。使用以下代码,我得到了最接近我实际想要实现的结果:
json['data']['offerAggregate']['property_aggregate']['property']['floors'][0]['units']['type_code' == 'bedroom']
但是,我仍然得到了与“厨房”相关的字典,而不是“卧室”(我猜是因为它是第一个列表条目)。
我也可以轻松地使用位置索引进行切片,但是列表中字典的位置对于其他公寓可能不同,我需要一种通用方法。
这是我未能进一步切片的完整架构:
{'units': [{'type_code': 'kitchen',
'features': [{'Code': 'fridge', 'Exists': True},
{'Code': 'freezer', 'Exists': True},
{'Code': 'oven', 'Exists': True},
{'Code': 'stove', 'Exists': True},
{'Code': 'pots-pans', 'Exists': True},
{'Code': 'dishes-cutlery', 'Exists': True},
{'Code': 'microwave', 'Exists': True},
{'Code': 'washing-machine', 'Exists': True},
{'Code': 'window', 'Exists': True},
{'Code': 'balcony', 'Exists': False},
{'Code': 'table', 'Exists': True},
{'Code': 'chairs', 'Exists': False},
{'Code': 'dryer', 'Exists': False}],
'subunits': None},
{'type_code': 'bathroom',
'features': [{'Code': 'bathtub', 'Exists': False},
{'Code': 'shower', 'Exists': True},
{'Code': 'sink', 'Exists': True},
{'Code': 'toilet', 'Exists': True},
{'Code': 'window', 'Exists': False}],
'subunits': None},
{'type_code': 'living-room',
'features': [{'Code': 'chairs', 'Exists': True},
{'Code': 'sofa', 'Exists': True},
{'Code': 'sofa-bed', 'Exists': False},
{'Code': 'window', 'Exists': True},
{'Code': 'balcony', 'Exists': True},
{'Code': 'coffee-table', 'Exists': True},
{'Code': 'table', 'Exists': True},
{'Code': 'tv', 'Exists': True}],
'subunits': None},
{'type_code': 'bedroom',
'features': [{'Code': 'wardrobe', 'Exists': True},
{'Code': 'chest-of-drawers', 'Exists': True},
{'Code': 'sofa', 'Exists': False},
{'Code': 'sofa-bed', 'Exists': False},
{'Code': 'window', 'Exists': True},
{'Code': 'balcony', 'Exists': False}],
'subunits': [{'id': 'd63d98a7-a5a6-47e9-8754-4b89750e22a5',
'type_code': 'double-bed',
'features': None}]}]}
所以,总的来说,我相信我确实了解我的数据模式。但是我所有正确切片的试验都失败了。
对此的任何建议将不胜感激!
一切顺利, 汉娜
【问题讨论】:
-
您能否改为发布数据的完整模式(而不是发布您未能检索数据的点)?
标签: python list dictionary slice