我有一个嵌套列表和字典树,我需要递归遍历并删除符合特定条件的整个字典.例如,我需要删除所有没有子项的"类型""文件夹"的词典(或一个空的子列表).
我仍然是一个初学者Pythonist所以请原谅蛮力.
这是一个格式化的示例字典,便于复制和粘贴.
{'children': [{'children': [{'key': 'group-1',
'name': 'PRD',
'parent': 'dc-1',
'type': 'Folder'},
{'children': [{'key': 'group-11',
'name': 'App1',
'parent': 'group-2',
'type': 'Folder'}],
'key': 'group-2',
'name': 'QA',
'parent': 'dc-1',
'type': 'Folder'},
{'key': 'group-3',
'name': 'Keep',
'parent': 'dc-1',
'type': 'Host'}],
'key': 'dc-1',
'name': 'ABC',
'parent': 'root',
'type': 'Datacenter'}],
'key': 'root',
'name': 'Datacenters',
'parent': None,
'type': 'Folder'}
Run Code Online (Sandbox Code Playgroud)
在这本字典中,唯一应该保留的树是/ root/dc-1/group-3.应首先删除group-11文件夹,然后删除其父级(因为孩子不再在那里),等等.
我已经尝试了许多不同的递归方法,但似乎无法让它正常工作.任何帮助将不胜感激.
def cleanup(tree):
def inner(tree):
if isinstance(tree, dict):
if 'type' in tree and tree['type'] == 'Folder':
if 'children' not in tree or not tree['children']: …Run Code Online (Sandbox Code Playgroud) 在 vim 中使用Ctrl-E和时Ctrl-Y,我希望它滚动多行而不是一次滚动一行。我如何设置我的 vimrc 来指定这两个命令的行数?