我试图匹配两个字典(一个嵌套)中的键,如果它们匹配,请向嵌套字典的内部字典中添加一个值。
我正在使用的代码是:
dict_z ={}
for key, value in full_dict.items():
match_name_full_1 = key[:-3]
match_name_full_2 = key[:-4]
for key_path, value_path in paths_dict.items():
if match_name_full_1 == key_path or match_name_full_2 == key_path:
dict_z.update({key: value, 'path' : value_path})
print(dict_z)
但是我得到的输出是
'Key': {x: 1, y: 1}, z: 1
我想何时:
'Key': {x: 1, y: 1, z: 1}
任何帮助将不胜感激。
答案 0 :(得分:0)
我找到了解决方法
dict_z ={}
for key, value in full_dict.items():
match_name_full_1 = key[:-3]
match_name_full_2 = key[:-4]
for key_path, value_path in paths_dict.items():
if match_name_full_1 == key_path or match_name_full_2 == key_path:
value['path'] = value_path
print(type(value))
dict_z.update({key: value})
不是最漂亮,但可以。