简体   繁体   English

更新嵌套字典中的列表

[英]Update a list in a nested dictionary

I have a nested dicts.我有一个嵌套的字典。 Here is my dict:这是我的字典:

org_query = {"query": {"bool": {"must": [],"must_not": []}}}

I want to update another dict inside the nested dict.我想更新嵌套字典中的另一个字典。 Here's the dict i want to add:这是我要添加的字典:

query_form =  { "match_phrase": { "name": "steve" }}

Required Output:所需输出:

org_query = {"query": {"bool": {"must": [{ "match_phrase": { "name": "steve" }],"must_not": []}}}

I found this Update value of a nested dictionary of varying depth but it updates the value.我发现了一个不同深度的嵌套字典的更新值,但它更新了该值。 In my case, i want to update the entire dict to the list in the key of the nested dict.就我而言,我想将整个 dict 更新为嵌套 dict 键中的列表。 How to make it possible.如何使它成为可能。

In your case, that would be:在你的情况下,那将是:

org_query["query"]["bool"]["must"].append(query_form)

More here: https://www.w3schools.com/python/python_dictionaries.asp更多信息: https : //www.w3schools.com/python/python_dictionaries.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM