3

给定一个字符串列表,如下所示:

a_list_of_keys = ['a key', 'heres another', 'oh hey a key']

什么是从字典中检索嵌套的一系列键的方法

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

1 回答 1

11

reduce与 一起使用operator.getitem

演示:

>>> from operator import getitem
>>> d = {'a': {'b': {'c': 100}}}
>>> reduce(getitem, ['a', 'b', 'c'], d)
100
>>> d['a']['b']['c']
100
于 2013-11-01T20:03:15.083 回答