【问题标题】:Access item in nested dictionary with list items and sub-dictionaries使用列表项和子字典访问嵌套字典中的项
【发布时间】: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


【解决方案1】:

您可以在列表理解中过滤卧室,例如:

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

【讨论】:

    【解决方案2】:

    字典键查找和列表索引查找都没有魔法。假设(为了可读性)我们首先定义

    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 是一个字典列表,因此您必须遍历它以找到第一个字典,其中 'typecode' 键具有 'bedroom' 的值:

    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 可能有这个)

    【讨论】:

      猜你喜欢
      • 2013-01-19
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多