首页 文章

比较未排序的字典值以及键[重复]

提问于
浏览
0

这个问题在这里已有答案:

我们有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 中的任何新的 Keyold_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 回答

  • 1

    这不是简单的:

    old_x = new_x
    

    你可以制作这样的副本:

    old_x = dict(new_x)
    
  • 0

    如果数组中键/值的顺序不重要,为什么不调用:

    old_x.update(new_x)

相关问题