我有一个 Python 多维字典,我有一个列表,其中包含我想要访问的键。从字典中获取值的最简单方法是什么?
例子:
main = {
'one': {
'two': {
'three': "Final word"
}
}
}
mylist = ['one', 'two', 'three']
# and I want to print out the value of `three` ("Final word")
最佳答案
遍历 mylist
,存储一个中间字典(我称之为 submain
),直到用完 mylist
元素:
submain = main
for key in mylist:
submain = submain[key]
print submain
关于Python - "map"字典的键列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22210071/