我想首先合并两个词典,这意味着第一个字典不会丢失其先前的值,并且只有丢失的键值被添加到第一个值中。
Original = {key1: { key2:[{ key3: y1 , key5: z1 , key6 [{key7: n1}]}]}}
Example = {key1: { key2:[{ key3: None , key4: None ,key5: None , key6 [{key7: None,key8:None}]}]}}
def update2(final, first):
for k, v in first.items():
if isinstance(v, str):
final[k] = v
elif k in final:
if isinstance(final[k], dict):
final[k].update2(first[k])
else:
final[k] = first[k]
else:
final[k] = first[k]
final_json = update2(Example['key1'], Original['key1'])
print(final_json)
#returns only Original not appended output
def update3(right,left):
d = dict()
for key,val in left.items():
d[key]=[val]
for key,val in right.items():
if key not in d.keys():
d[key].append(val)
else:
d[key]=[val]
final_json = update3(Example['key1'], Original['key1'])
print(final_json) #returns None
预期输出:
{key1:{key2:[{key3:y1,key4:none,key5:z1,ke1,key6 [{key7:n1,key8:none}]}}}}}}}}}}}}}}}}}}}}}}
}它必须在多个层面上迭代。
根据JSON Schema的设置默认值
” https://stackoverflow.com/questions/55694862/how-to-to-merge-two-dictionaries基于-n-key-beke-but-include-missing-missing-key-key-key-vorues-in-pyt"> in> wought"> wough>在键上,但在python中包括丢失的密钥值?
合并两个词典并坚持第一个字典的值
我的目标是在示例文件中添加丢失键的默认值。我是初学者到python。
I want merge two dictionaries first over second, meaning the first dictionary doesn't lose its previous values and only the missing key values are added into first.
Original = {key1: { key2:[{ key3: y1 , key5: z1 , key6 [{key7: n1}]}]}}
Example = {key1: { key2:[{ key3: None , key4: None ,key5: None , key6 [{key7: None,key8:None}]}]}}
def update2(final, first):
for k, v in first.items():
if isinstance(v, str):
final[k] = v
elif k in final:
if isinstance(final[k], dict):
final[k].update2(first[k])
else:
final[k] = first[k]
else:
final[k] = first[k]
final_json = update2(Example['key1'], Original['key1'])
print(final_json)
#returns only Original not appended output
def update3(right,left):
d = dict()
for key,val in left.items():
d[key]=[val]
for key,val in right.items():
if key not in d.keys():
d[key].append(val)
else:
d[key]=[val]
final_json = update3(Example['key1'], Original['key1'])
print(final_json) #returns None
Expected output:
{key1: { key2:[{ key3: y1 , key4: None ,key5: z1 , key6 [{key7: n1,key8:None}]}]}}
I have referred many stackoverlfow posts but nothing is working since it has to be iterated at multiple levels.
Set default values according to JSON schema automatically
how to merge two dictionaries based on key but include missing key values in python?
Merge two dictionaries and persist the values of first dictionaries
My goal is to add default values for missing keys from example file.I am beginner to Python.
发布评论
评论(2)
尝试在以下步骤中递归处理。
这样的,那么
Try processing it recursively, in the following steps.
like this:
我以为没有列表,并且不希望将键中的键添加到示例中。我不确定这是您要寻找的答案,但这是一个建议。
Python
I assumed that there was no list and that it was not desired that a key present in Original but not present in Example be added to Example. I'm not sure this is the answer that you are looking for, but here is a proposal.
python