【发布时间】:2017-06-30 06:43:11
【问题描述】:
我有一个嵌套字典和键值的字典。
dicta = {'key1': {'keya': 'a', 'keyb': 'b'},
'key2': {'key3':{'keyc': 'c'}}, 'key4': 4}}
有没有一种方法可以使用 for 循环到达“c”?
我试过了,
print(dicta['key2'])
for x in dicta['key2']:
for y in x['key3']:
print(y)
对于第一次打印,我得到了
{'key3': {'keyc': 'c'}}
但我得到 TypeError: string indices must be integers for the second print.
提前致谢! *我编辑了用dicta替换n;我第一次复制粘贴错了。
【问题讨论】:
-
看起来您不应该在这里迭代。你可能只想要
dicta['key2']['key3']。
标签: python python-3.x dictionary