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

时间:2018-11-07 15:54:05

标签: python list dictionary slice

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

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

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

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

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

祝一切顺利, 汉娜

2 个答案:

答案 0 :(得分:1)

在dict键查找和列表索引查找中没有任何魔术。假设(出于可读性考虑)我们首先定义

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

或使用一些知道如何进行此类查找的第三方库(不确定,但熊猫可能会这样做)

答案 1 :(得分:1)

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

public function somefunction($parameters = [])
{
  $value = $parameters['value'];

  if ($parameters['decodeAlias']) {
     return preg_replace('#^page([0-9]*)$#i', '$1', $value);
  } else {
     return 'page' . $value;
  }
}

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

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

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

for bedroom in bedrooms:
    bedroom['features']