在python中映射嵌套字典

时间:2014-05-12 21:56:01

标签: python dictionary

我正在转换XML文档中的字段,因此我可以加载到普通的关系数据库中。我已经将XML文档转换为一堆嵌套的词典。我希望提取的一些值是嵌套字典,所以我需要先将其展平。

很简单,但我想创建一个映射,让我可以预先指定要提取的内容。

实施例

input_dict = {
'authors': [{'name': u'Google, Inc.'}],
'islink': False,
}

mapping = ['islink',<???>]

所需的输出

In: tuple(input_dict[key] for key in mapping)
Out: (False, 'Google, Inc.')

这显然不起作用:

In: [input_dict[key] for key in ['islink',['authors'][0]['name']]]
Out: TypeError: string indices must be integers, not str

2 个答案:

答案 0 :(得分:2)

以及如何:

from collections import Iterable

def flatten(x):
    result = []
    if isinstance(x, dict):
        x = x.values()
    for el in x:
        if isinstance(el, Iterable) and not isinstance(el, str):
            result.extend(flatten(el))
        else:
            result.append(el)
    return result

其中,这次是python3友好的; - )

>>> dd = {'a': 42, 'c': 12, 'b': [{1: 2, 2: 3, 3: 4}]}
>>> flatten(dd)
[42, 12, 2, 3, 4]

这是支持密钥过滤的版本:

def flatten(x, keys=None):
    result = []
    if isinstance(x, dict):
        if keys is None:
            x = x.values()
        else:
            x = dict(filter(lambda t: t[0] in keys, x.items())).values()
    for el in x:
        if isinstance(el, Iterable) and not isinstance(el, str):
            result.extend(flatten(el, keys))
        else:
            result.append(el)
    return result

结果:

>>> flatten(dd, keys=['a'])
[42]
>>> flatten(dd, keys=['a','b']) # even though 'b' is selected, no sub key has been
[42]
>>> flatten(dd, keys=['a','b',1]) # to get a subkey, 'b' needs to be selected
[42, 2]
>>> flatten(dd,keys=['a',1]) # if you don't then there's no subkey selected
[42]
>>> flatten(dd, keys=['a','b',2,3])
[42, 3, 4]

以及您的使用案例:

>>> input_dict = {'authors': [{'name': 'Google, Inc.'}],'islink': False,}
>>> flatten(input_dict)
[False, 'Google, Inc.']

N.B。:我根据that answer about list flattening

调整了答案

答案 1 :(得分:1)

这个怎么样:

indices = [['islink',], ['authors', 0, 'name']]
result = []
for index in indices:
  value = input_dict
  for single_index in index:
    value=value[single_index]
  result.append(value)