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的存在。谢谢@达斯汀·怀亚特很高兴这有帮助。:-)