如何从给定的键列表中的嵌套字典中检索值?

Dus*_*att 3 python dictionary list python-2.7

给出一个像这样的字符串列表:

a_list_of_keys = ['a key', 'heres another', 'oh hey a key']
Run Code Online (Sandbox Code Playgroud)

什么是从dict中检索嵌套系列键的方法

the_value_i_want = some_dict['a key']['heres another']['oh hey a key']
Run Code Online (Sandbox Code Playgroud)

Ash*_*ary 11

使用reduceoperator.getitem.

演示:

>>> from operator import getitem
>>> d = {'a': {'b': {'c': 100}}}
>>> reduce(getitem, ['a', 'b', 'c'], d)
100
>>> d['a']['b']['c']
100
Run Code Online (Sandbox Code Playgroud)