从嵌套字典中抓取值

时间:2013-11-13 20:42:02

标签: python idiomatic

是否有一种从嵌套字典中获取值的标准方法?这个函数写起来比较容易,但是我很好奇PSL或者语言本身是否已经可以这样使用了?

这是我的意思的例子:

def grab_from_dict(d, *keys):
    assert isinstance(d, dict), 'd must be of type dict'

    current_dict = d
    for key in keys[0 : -1]:
        if key not in current_dict:
            return None

        current_dict = current_dict[key]

    if current_dict is None:
        return None

    return current_dict.get(keys[-1], None)

d = {
    'a' : {
        'b' : {
           'c1' : {
                'd' : 'leeloo'
            },
           'c2' : {
                'd' : None
            },
           'c3' : {
                'e' : None
            },
            'c4' : None
        }
    }
}

print grab_from_dict(d, 'a', 'b', 'c1')
> {'d': 'leeloo'}
print grab_from_dict(d, 'a', 'b', 'c1', 'd')
> leeloo
print grab_from_dict(d, 'a', 'b', 'c2')
> {'d': None}
print grab_from_dict(d, 'a', 'b', 'c2', 'd')
> None
print grab_from_dict(d, 'a', 'b', 'c3')
> {'e': None}
print grab_from_dict(d, 'a', 'b', 'c3', 'd')
> None
print grab_from_dict(d, 'a', 'b', 'c4')
> None
print grab_from_dict(d, 'a', 'b', 'c4', 'd')
> None
print grab_from_dict(d, 'a', 'b', 'c5')
> None
print grab_from_dict(d, 'a', 'b', 'c5', 'd')
> None

这为我提供了一种在嵌套字典中深入获取值的方法,而无需担心父字典的存在。所以不要写这个:

value = None
if 'a' in d and d['a'] not None:
    if 'b' in d['a'] and d['a']['b'] is not None:
        if 'c1' in d['a']['b'] and d['a']['b']['c1'] is not None:
            value = d['a']['b']['c1'].get('d', None)
print value
> leeloo

我可以这样写:

value = grab_from_dict(d, 'a', 'b', 'c1', 'd')
print value
> leeloo

如果缺少任何父级,则该函数只返回None:

value = grab_from_dict(d, 'a', 'a', 'c1', 'd')
print value
> None

3 个答案:

答案 0 :(得分:1)

捕捉异常应该有效:

try:
    result = d['a']['b']['c1']
except KeyError:
    result = None

答案 1 :(得分:1)

您可以编写/查找使用此类行为编写的自定义容器类(如果您尝试获取密钥,可能有一个返回自身的NoneDict对象?),但更优雅的解决方案可能是try / except块:

try:
    x = d['a']['b']['c5']['d']
except KeyError:
    x = None

因为这只是告诉程序如何处理预期的错误。我会称这个小代码阻止了“pythonic”方式。

答案 2 :(得分:0)

你为什么要找功能?这不包括你要找的东西吗?

>>> d['a']['b']['c1']
{'d': 'leeloo'}
>>> d['a']['b']['c1']['d']
'leeloo'
相关问题