How to merge dictionaries of dictionaries?

后端 未结 29 3201
渐次进展
渐次进展 2020-11-22 05:13

I need to merge multiple dictionaries, here\'s what I have for instance:

dict1 = {1:{\"a\":{A}}, 2:{\"b\":{B}}}

dict2 = {2:{\"c\":{C}}, 3:{\"d\":{D}}
         


        
29条回答
  •  独厮守ぢ
    2020-11-22 05:53

    You could try mergedeep.


    Installation

    $ pip3 install mergedeep
    

    Usage

    from mergedeep import merge
    
    a = {"keyA": 1}
    b = {"keyB": {"sub1": 10}}
    c = {"keyB": {"sub2": 20}}
    
    merge(a, b, c) 
    
    print(a)
    # {"keyA": 1, "keyB": {"sub1": 10, "sub2": 20}}
    

    For a full list of options, check out the docs!

提交回复
热议问题