这个问题在这里已有答案:
我们有2个字典,我想申请以下条件并用 new_x
更新 old_x
old_x = {"A":[1,2,3,4,5],"B":[],"C":[10,30]}
new_x = {"B":[3,4,5],"A":[1,5,3],"C":[30,10],"D":[2]}
在 lists
值内_2730__的
-
排序并不重要 .
ex: "C"
-
在字典里面
Keys
的排序并不重要 .ex: un-ordered keys in new_x
-
old_x
中的任何新的Key
(old_x
中不存在)应添加到old_x
.ex: "D"
-
如果在
new_x
列表中找到新元素,则使用new_x
值更新old_x
值列表,如果new_x
不包含old_x
的值,则类似地删除values
.ex: "B" for updating, "A" for removing
比较时的预期产出:
old_x = {“A”:[1,5,3],“B”:[3,4,5],“C”:[10,30],“D”:[2]}
请帮我创建一个功能吗?
Note: Elements & Keys in new_x is not always the same.I Kept them similar to demonstrate the keypoints of the problem.
2 回答
这不是简单的:
你可以制作这样的副本:
如果数组中键/值的顺序不重要,为什么不调用:
old_x.update(new_x)