繁体   English   中英

使用列表项和子词典访问嵌套字典中的项

[英]Access item in nested dictionary with list items and sub-dictionaries

我有一本包含字典列表的字典,该字典又包含列表。 该词典包含有关某个房间的房间类型中是否存在某些特征(布尔)的信息。 我想提取与其中一个房间有关的字典-例如“卧室”。

我尝试了几种切片方法。 使用以下代码,我得到了与实际想要实现的结果最接近的结果:

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}]}]}

因此,总的来说,我相信我确实了解我的数据模式。 但是我所有正确切片的尝试都失败了。

任何建议对此将不胜感激!

一切顺利,汉娜

字典键查找和列表索引查找都没有任何魔力。 假设(出于可读性考虑)我们首先定义

units = json['data']['offerAggregate']['property_aggregate']['property']['floors'][0]['units'])

那这个

something = units['type_code' == 'bedroom']

被解释为

index = 'type_code' == 'bedroom'
something = unit[index]

由于文字字符串“ type_code”不等于文字字符串“ bedroom”,因此等于:

index = False
something = unit[index]

并且由于False == 0 ,最终结果是

something = unit[0]

这是第一个字典(typecode =>'kitchen')。

units是字典列表,因此您必须遍历它以查找第一个字典,其中“类型代码”键的值具有“卧室”:

bedroom == None

for stuff in units:
    if stuff["typecode"] == "bedroom":
        bedroom = stuff
        break

您当然可以使它成为一个函数(带有“单位”列表和类型代码)。

如果类型代码被保证是唯一的,另一个解决方案是构建索引字典:

units_by_typecode = {item['typecode']: item for item in units}
bedroom = units_by_typecode["bedroom"]

或使用一些知道如何进行此类查找的第三方库(不确定,但Panda可能具有此功能)

您可以按照以下列表过滤条件过滤卧室:

bedrooms = [i for i in data['units'] if i['type_code'] == 'bedroom']

然后,您可以通过以下方式访问功能:

for bedroom in bedrooms:
    bedroom['features']

如果只有一间卧室,则可以遍历列表,直到与之匹配为止:

for room in data['units']:
    if room['type_code'] == 'bedroom':
        # bedroom found
        room['type_code']['features']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM