python - 迭代嵌套字典

标签 python

有没有一种简单的方法来迭代嵌套字典,它可能包含其他对象,如列表、元组,然后是字典,以便迭代覆盖这些其他对象的所有元素?

例如,如果我键入一个嵌套字典对象的键,我会将其全部列在 Python 解释器中。


[编辑]这里是字典示例:

{
'key_1': 'value_1',
'key_2': {'key_21': [(2100, 2101), (2110, 2111)],
      'key_22': ['l1', 'l2'],
      'key_23': {'key_231': 'v'},
      'key_24': {'key_241': 502,
             'key_242': [(5, 0), (7, 0)],
             'key_243': {'key_2431': [0, 0],
                 'key_2432': 504,
                 'key_2433': [(11451, 0), (11452, 0)]
                },
             'key_244': {'key_2441': {'key_24411': {'key_244111': 'v_24411',
                                'key_244112': [(5549, 0)]
                               },
                          'key_24412':'v_24412'
                         },
                 'key_2441': ['ll1', 'll2']
                }
            },
     }
}

对不起,我读不明白,但我已尽我所能。

最佳答案

def recurse(d):
  if type(d)==type({}):
    for k in d:
      recurse(d[k])
  else:
    print d

关于python - 迭代嵌套字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8335096/

相关文章:

python - 向 DataFrame 添加列是否涉及复制数据?

python - 让 BeautifulSoup 跳过我的 html 的一部分..或拆分它

python - 使用OpenCV退出代码-1073741819(0xC0000005)

python - 使用 Pyramid 服务文件

python - 有两个程序的 GUI 类

python - 导入错误: cannot import name 'SimpleImputer'

python - 如何使用 python pandas 处理我的数据集?

python - 在为 Android 应用程序制作 Tensorflow lite 模型以便显示输出时,我需要做些什么特别的事情吗?

python - 字典列表中的 Nan 值

python - 为 celery 任务编写测试时出错