【发布时间】:2020-10-09 19:48:22
【问题描述】:
我正在努力思考如何导航嵌套在列表中的一系列字典。
例如:mydict = {'first':[{'second':2, 'third':3}, {'fourth':4}]}
当我输入mydict.get('first') 时,我会得到整个列表。
我不能使用索引来获取列表中的每个单独的字典(即mydict.get(['first'][0] 返回整个列表,mydict.get(['first'][1]) 返回一个 IndexError)。
mydict.get(['first'][0]['second']) 和mydict.get(['first']['second']) 返回类型错误。
那么,如果我想调用 'second' 或 'fourth' 或将它们的值分配给变量,我该怎么做呢?
【问题讨论】:
标签: python dictionary nested