【问题标题】:Merge two dictionaries in Python with the same keys在 Python 中使用相同的键合并两个字典
【发布时间】:2017-12-18 13:24:36
【问题描述】:

我有两个字典,它们的键完全相同。 第一个字典是:

 { "key_1" : "AR" ,
   "key_2":"BR" ,
   "key_3" : "CR" }

第二个是:

{ "key_1" : "signinfication of AR" ,
  "key_2":" signinfication of  BR" ,
  "key_3" : " signinfication of  CR" }

我想获得下面的字典:

{"key_1" : {"AR" : "signinfication of AR"} ,
 "key_2" : {"BR" : "signinfication of BR"} ,
 "key_3" : {"CR" : "signinfication of CR"}

感谢您的帮助!

【问题讨论】:

    标签: python dictionary merge


    【解决方案1】:

    这就像一行dict理解一样简单。

    >>> {k : {d1[k]  : d2[k]} for k in d1.keys() & d2.keys()}
    {
        "key_2": {
            "BR": " signinfication of  BR"
        },
        "key_1": {
            "AR": "signinfication of AR"
        },
        "key_3": {
            "CR": " signinfication of  CR"
        }
    }
    

    这里,d1d2 是您的两个字典。 d1.keys() & d2.keys() 将对字典键执行交集,以确保对两个字典中都存在的键进行迭代。这里,就是

    d1.keys() & d2.keys()
    {'key_1', 'key_2', 'key_3'}
    

    当您不能保证两个字典具有完全相同的键时,一般来说这是很好的。


    在 python2.7 和更早版本上,您需要稍作修改,因为keys() 返回一个列表。使用set.intersection -

    >>> {k : {d1[k]  : d2[k]} for k in set(d1.keys()).intersection(d2.keys())}
    

    如果您正在使用列表的字典,那么上面的代码需要在相应列表之间使用zipping -

    >>> d1
    {
        "key_1": [
            "AR",
            "BR",
            "CR"
        ],
        ...
    }    
    >>> d2
    {
        "key_1": [
            "signfication of AR",
            "signfication of BR",
            "signfication of  CR"
        ],
        ...
    }
    
    >>> {k : dict(zip(d1[k], d2[k])) for k in d1.keys() & d2.keys()}
    {
        "key_1": {
            "BR": "signfication of BR",
            "CR": "signfication of  CR",
            "AR": "signfication of AR"
        },
        "key_3": {
            "CE": " signfication of CE",
            "AE": "signfication of AE",
            "BE": " signfication of BE"
        },
        "key_2": {
            "BZ": "signfication of BZ",
            "CZ": "signfication of CZ",
            "AZ": "signfication of AZ"
        }
    }
    

    【讨论】:

      【解决方案2】:

      您也可以将zip() 和字典items() 合并在一起:

      d1 = {"key_1" : "AR",
            "key_2":"BR",
            "key_3" : "CR"}
      
      d2 = {"key_1" : "signinfication of AR",
            "key_2":" signinfication of  BR",
            "key_3" : " signinfication of  CR"}
      
      # make sure both lists have same ordered keys
      l1 = sorted(d1.items())
      l2 = sorted(d2.items())
      
      d = {k1 : {v1:v2} for (k1, v1), (_, v2) in zip(l1, l2)}
      
      print(d)
      

      哪些输出:

      {'key_1': {'AR': 'signinfication of AR'}, 
       'key_2': {'BR': ' signinfication of  BR'}, 
       'key_3': {'CR': ' signinfication of  CR'}}
      

      编辑:

      按照@cᴏʟᴅsᴘᴇᴇᴅ 的建议,您可以在压缩列表之前调用列表中的sorted,以确保两个字典具有相同的顺序:key_1key_2key_3。您还可以对键进行初步检查,例如检查它们的交集,以确保两个字典具有相同的键。

      【讨论】:

      • 你能保证从两个字典中返回的键顺序相同吗?
      • @cᴏʟᴅsᴘᴇᴇᴅ 嗯,我没想到,对不起。我想 OP 可以在您的回答中使用您的建议来确保发生这种情况。无论如何我会添加这个,谢谢。
      • 保证这一点没什么大不了的,只需致电sorted,它应该工作。点赞,干杯。
      【解决方案3】:

      你可以试试这个:

      s = { "key_1" : "AR" ,
       "key_2":"BR" ,
       "key_3" : "CR" }
      
      d = { "key_1" : "signinfication of AR" ,
       "key_2":" signinfication of  BR" ,
       "key_3" : " signinfication of  CR" }
      new_d = {a:{b:d[a]} for a, b in s.items()}
      

      输出:

      {'key_1': {'AR': 'signinfication of AR'}, 'key_3': {'CR': ' signinfication of  CR'}, 'key_2': {'BR': ' signinfication of  BR'}}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-12
        • 2013-08-17
        • 1970-01-01
        • 1970-01-01
        • 2020-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多