【发布时间】:2020-08-18 03:32:42
【问题描述】:
这是我的 JSON 数据。 JSON 数据包含一些属性,但我只需要命令和子命令属性。
{
command: 'abc',
depth: 1,
help: '...',
subcommands:[
{
command: 'abc folder',
depth: 2,
help: '...',
subcommands:[
{
command: 'abc folder add',
depth: 3,
help: '...',
subcommands:[
{
command: 'abc folder add id',
depth: 4,
help: '...',
subcommands: []
},
{
command: 'abc folder add type',
depth: 4,
help: '...',
subcommands: []
},
{
command: 'abc folder add name',
depth: 4,
help: '...',
subcommands: []
}]
}],
{
command: 'abc folder list',
depth: 3,
help: '...',
subcommands: []
},
{
command: 'abc folder view',
depth: 3,
help: '...',
subcommands: [
{
command: 'abc folder view id',
depth: 4,
help: '...',
subcommands: []
},
{
command: 'abc folder view type',
depth: 4,
help: '...',
subcommands: []
},
{
command: 'abc folder view name',
depth: 4,
help: '...',
subcommands: []
}
}]
}]
}
我想从 JSON 数据中检索的嵌套字典如下:
{
'abc':
{
'folder':
{
'add':
{
'id': {},
'type': {},
'name': {}
},
'list': {},
'view':
{
'id': {},
'type': {},
'name': {}
}
}
}
}
我需要使用递归函数,以便它适用于更深层次的属性。 它只需要使用命令和子命令。 如果有子命令的值是空的,那么在结果中,相关属性的值也应该是空的。 请发布您的建议,以有效地制作嵌套 python 字典。
我们将不胜感激您的所有回答。 谢谢。
【问题讨论】:
标签: python dictionary recursion nested