更新嵌套字典 python
Update nested dictionary python
更新嵌套字典的正确方法是什么,所以我有两个字典
data = {
'version': '2',
'workflow': {'task_defaults': {'retry': {'count': 3, 'delay': 2}},
'tasks': {'t1': {'action': 'postgres.export-db',
'input': {'database': 'prod',
'filepath': '/var/tmp/prod-dump.pgsql',
'host': 'postgres.local',
'password': 'mypass',
'username': 'myuser'},
'on-success': ['t2']},
't2': {'action': 'aws.upload-to-s3',
'input': {'sourcepath': '{{ tasks(t1).result.filepath }}',
'targetpath': 's3://mybucket/prod-dump.pgsql'},
'on-success': ['t3'],
'retry': {'count': 5, 'delay': 5}},
't3': {'action': 'shell.command',
'input': {'cmd': 'rm {{ tasks(t1).result.filepath }}'},
'on-complete': ['t4']},
't4': {'action': 'notify.send-mail',
'input': {'from': 'bot@njinn.io',
'message': 'DB Dump {{ tasks(t1).result.filepath }} was stored to S3',
'subject': 'Prod DB Backup',
'to': 'admin@njinn.io'},
'target': 'njinn'}}}}
和另一个
new_data = {'action': '/srv/foo/', 'pack': 'name'}
所以我想将 new_data
合并到 input
dict inside data
.
所以 input
看起来像这样
{"input": {
"action": "/srv/foo",
"pack": "name",
"target": "foo",
"parameters": {
"to": "admin@foo.io",
"from": "bot@foo.io",
"subject": "Prod DB Backup",
"message": "DB Dump was stored to S3"
}
}
我怎样才能做到这一点,所有尝试都失败了,我拥有的最佳解决方案是
{key:dict(data.get(key,{}), **values) for key,values in new_data.items()}
但这不起作用,谁能解释一下如何做到这一点,谢谢。
就用双星符号吧!如果你只是想合并这两个命令:
for key in data['workflow']['tasks']:
data['workflow']['tasks'][key]['input'] = {**new_data, **data['workflow']['tasks'][key]['input']}
如果你想让字典看起来像你的例子(parameters
):
for key in data['workflow']['tasks']:
data['workflow']['tasks'][key]['input'] = {**new_data, 'parameters': data['workflow']['tasks'][key]['input']}
更新嵌套字典的正确方法是什么,所以我有两个字典
data = {
'version': '2',
'workflow': {'task_defaults': {'retry': {'count': 3, 'delay': 2}},
'tasks': {'t1': {'action': 'postgres.export-db',
'input': {'database': 'prod',
'filepath': '/var/tmp/prod-dump.pgsql',
'host': 'postgres.local',
'password': 'mypass',
'username': 'myuser'},
'on-success': ['t2']},
't2': {'action': 'aws.upload-to-s3',
'input': {'sourcepath': '{{ tasks(t1).result.filepath }}',
'targetpath': 's3://mybucket/prod-dump.pgsql'},
'on-success': ['t3'],
'retry': {'count': 5, 'delay': 5}},
't3': {'action': 'shell.command',
'input': {'cmd': 'rm {{ tasks(t1).result.filepath }}'},
'on-complete': ['t4']},
't4': {'action': 'notify.send-mail',
'input': {'from': 'bot@njinn.io',
'message': 'DB Dump {{ tasks(t1).result.filepath }} was stored to S3',
'subject': 'Prod DB Backup',
'to': 'admin@njinn.io'},
'target': 'njinn'}}}}
和另一个
new_data = {'action': '/srv/foo/', 'pack': 'name'}
所以我想将 new_data
合并到 input
dict inside data
.
所以 input
看起来像这样
{"input": {
"action": "/srv/foo",
"pack": "name",
"target": "foo",
"parameters": {
"to": "admin@foo.io",
"from": "bot@foo.io",
"subject": "Prod DB Backup",
"message": "DB Dump was stored to S3"
}
}
我怎样才能做到这一点,所有尝试都失败了,我拥有的最佳解决方案是
{key:dict(data.get(key,{}), **values) for key,values in new_data.items()}
但这不起作用,谁能解释一下如何做到这一点,谢谢。
就用双星符号吧!如果你只是想合并这两个命令:
for key in data['workflow']['tasks']:
data['workflow']['tasks'][key]['input'] = {**new_data, **data['workflow']['tasks'][key]['input']}
如果你想让字典看起来像你的例子(parameters
):
for key in data['workflow']['tasks']:
data['workflow']['tasks'][key]['input'] = {**new_data, 'parameters': data['workflow']['tasks'][key]['input']}