<分区>
我有一个树结构,由嵌套字典构建:
{
'test': {
'Data': {},
},
'Test': {
'data': {
'Other': {},
},
},
}
我想将其转换为:
{
'test': {
'data': {
'other': {},
},
},
}
有什么方法可以在 python 中执行此转换?
我坚持:所有的值都是字典。
<分区>
我有一个树结构,由嵌套字典构建:
{
'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': {}}}}
关于python - 嵌套字典的下键和合并数据(如果下键已经存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14554492/