Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从给定键列表的嵌套字典中检索值?_Python_List_Python 2.7_Dictionary - Fatal编程技术网

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

Python 如何从给定键列表的嵌套字典中检索值?,python,list,python-2.7,dictionary,Python,List,Python 2.7,Dictionary,给出如下字符串列表: a_list_of_keys = ['a key', 'heres another', 'oh hey a key'] 从类似dict的文件中检索嵌套的密钥序列的方法是什么 the_value_i_want = some_dict['a key']['heres another']['oh hey a key'] 与操作符一起使用reduce。getitem 演示: >>> from operator import getitem >>&g

给出如下字符串列表:

a_list_of_keys = ['a key', 'heres another', 'oh hey a key']
从类似dict的文件中检索嵌套的密钥序列的方法是什么

the_value_i_want = some_dict['a key']['heres another']['oh hey a key']

操作符一起使用
reduce
。getitem

演示:

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

美好的我想我需要像getitem这样的东西,而不知道getitem的存在。谢谢@达斯汀·怀亚特很高兴这有帮助。:-)