Python 嵌套字典和合并数据的下键(如果下键已存在)

Python 嵌套字典和合并数据的下键(如果下键已存在),python,json,dictionary,nested,Python,Json,Dictionary,Nested,我有一个树状结构,由嵌套字典构建: { 'test': { 'Data': {}, }, 'Test': { 'data': { 'Other': {}, }, }, } 我想将其转换为: { 'test': { 'data': { 'other': {}, }, }, } 有没有办法在python中执行此转换 我坚持:所有值都是字典。尝试递归函数调用,将键小写: >>> def low

我有一个树状结构,由嵌套字典构建:

{
  'test': {
    'Data': {},
  },
  'Test': {
    'data': {
      'Other': {},
    },
  },
}
我想将其转换为:

{
  'test': {
    'data': {
      'other': {},
    },
  },
}
有没有办法在python中执行此转换


我坚持:所有值都是字典。

尝试递归函数调用,将键小写:

>>> def lower_keys(tree):
        if not tree:
            return tree
        return {k.lower() : lower_keys(v) for k, v in tree.items()}

>>> t = {
  'test': {
    'Data': {},
  },
  'Test': {
    'data': {
      'Other': {},
    },
  },
}

>>> lower_keys(t)
{'test': {'data': {'other': {}}}}

尝试递归函数调用以将键小写:

>>> def lower_keys(tree):
        if not tree:
            return tree
        return {k.lower() : lower_keys(v) for k, v in tree.items()}

>>> t = {
  'test': {
    'Data': {},
  },
  'Test': {
    'data': {
      'Other': {},
    },
  },
}

>>> lower_keys(t)
{'test': {'data': {'other': {}}}}

您遇到的问题是什么?如果已存在较低的键,则合并dicts时请参见。。。您只需要添加一些小写代码。您是否尝试过在json上使用小写代码,然后将其转换为字典?我不确定这是否有效;我在打电话,但如果不是这样,我会自己试试。只是想澄清一下:你想合并字典和小写键?你有什么问题?如果小写键已经存在,合并dicts时请参见。。。您只需要添加一些小写代码。您是否尝试过在json上使用小写代码,然后将其转换为字典?我不确定这是否有效;我在打电话,但如果不是这样,我会自己试试。只是想澄清一下:你想合并字典和小写键吗?