将具有嵌套字典的两个字典合并成新的,将相同的键相加,并在Python中保留未更改的字典 [英] Merge two dictionaries with nested dictionaries into new one, summing same keys and keeping unaltered ones in Python

查看:175
本文介绍了将具有嵌套字典的两个字典合并成新的,将相同的键相加,并在Python中保留未更改的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字典,包括一个嵌套字典,我想将它们合并在一个更大的字典中。两个词典中的一些键是不同的,其中一些是相同的。那些不同的,我只想复制到新的更大的字典。那些相同的东西,我想保持关键字从字典和嵌套的一个相同,并添加嵌套字典中的值。



示例数据:

  dict1 = {'C-STD-B& M-L':
{datetime.date ,4,30):0.06875104,
datetime.date(2015,5,1):0.07842368,
datetime.date(2015,5,2):0.08919679999999999,
datetime.date 2015,5,16):0.40798848},
'G-CAM-MAS-XXS':
{datetime.date(2015,4,30):0.0008190299999999999,
datetime.date 2015,5,1):0.00093426,
datetime.date(2015,5,2):0.0010626}

dict2 = {'C-STD-B& M-L':
{datetime.date(2015,5,16):0.075968,
datetime.date(2015,5,17):0.086656},
'H-QLD-BAC-STD':
{datetime.date(2015,5,16):0.17804999999999999,
d atetime.date(2015,5,17):0.2031,
datetime.date(2015,5,18):0.23099999999999998}

预期输出:

  new_dict = 
{'C-STD-B& M-L':
{datetime.date(2015,4,30):0.06875104,
datetime.date(2015,5,1):0.07842368,
datetime.date(2015, 5,2):0.08919679999999999,
datetime.date(2015,5,16):0.40798848 + 0.075968
datetime.date(2015,5,17):0.086656},
'G- CAM-MAS-XXS':
{datetime.date(2015,4,30):0.0008190299999999999,
datetime.date(2015,5,1):0.00093426,
datetime.date 2015,5,2):0.0010626
'H-QLD-BAC-STD':
{datetime.date(2015,5,16):0.17804999999999999,
datetime.date(2015, 5,17):0.2031,
datetime.date(2015,5,18):0.23099999999999998 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

$ p $ sum(0.48395648)。



我试图做一些循环来添加它们,但是它们真的很杂乱,我会为了这个而发布。 >

示例代码:

  all_unis_dict = {} 
for skus1,在unsw_mid_year_dict.items()中的subdict1:
for skus2,subdict2 in unsw_mid_year_dict.items():
for dates1,days1 in subdict1.items():
for dates2,days2 in subdict2.items ():
pprint(days1 + days2)

任何人都知道一个优雅的方式来做?

解决方案

看起来像一个defaultdict的defaultdict的int ...



基本思想是当您遇到不在顶级defaultdict中的密钥时,您将添加密钥(具有关联的 defaultdict(int)存储日期 - > in teger地图作为值)。当您遇到不在嵌套dict中的日期时, defaultdict(int)将添加默认值 0 (因为$ code> int()调用没有参数返回 0 )。



以下是一些代码,使其具体化:

 从集合import defaultdict 
输出= defaultdict(lambda:defaultdict(int))
for(dict1,dict2):
为key,values_dict为d.items():
为日期,整数为values_dict。 items():
output [key] [date] + = integer

想要彻底,事实上你可以将default_factory设置为 None ,以防止从 defaultdict

  output.default_factory = None 
for output.values()中的default_date_dict:
default_date_dict .default_factory =无


I have two dictionaries, both with a nested dictionary, and I want to merge them in a bigger dictionary. Some of the keys are different in the two dictionaries, some of them are the same. The ones that are different, I simply want to copy to the new bigger dictionary. The ones that are the same, I want to keep the key both from the dictionary and the nested one the same, and add the values from the nested dictionary.

Sample data:

dict1 = {'C-STD-B&M-L':
              {datetime.date(2015, 4, 30): 0.06875104,
               datetime.date(2015, 5, 1): 0.07842368,
               datetime.date(2015, 5, 2): 0.08919679999999999,
               datetime.date(2015, 5, 16): 0.40798848},
         'G-CAM-MAS-XXS': 
              {datetime.date(2015, 4, 30): 0.0008190299999999999,
               datetime.date(2015, 5, 1): 0.00093426,
               datetime.date(2015, 5, 2): 0.0010626}

dict2 = {'C-STD-B&M-L':
              {datetime.date(2015, 5, 16): 0.075968,
               datetime.date(2015, 5, 17): 0.086656},
         'H-QLD-BAC-STD': 
              {datetime.date(2015, 5, 16): 0.17804999999999999,
               datetime.date(2015, 5, 17): 0.2031,
               datetime.date(2015, 5, 18): 0.23099999999999998}

Expected output:

 new_dict =  
        {'C-STD-B&M-L':
              {datetime.date(2015, 4, 30): 0.06875104,
               datetime.date(2015, 5, 1): 0.07842368,
               datetime.date(2015, 5, 2): 0.08919679999999999,
               datetime.date(2015, 5, 16): 0.40798848 + 0.075968
               datetime.date(2015, 5, 17): 0.086656},
         'G-CAM-MAS-XXS': 
              {datetime.date(2015, 4, 30): 0.0008190299999999999,
               datetime.date(2015, 5, 1): 0.00093426,
               datetime.date(2015, 5, 2): 0.0010626
         'H-QLD-BAC-STD': 
              {datetime.date(2015, 5, 16): 0.17804999999999999,
               datetime.date(2015, 5, 17): 0.2031,
               datetime.date(2015, 5, 18): 0.23099999999999998}

I posted the value as a sum (0.40798848 + 0.075968) just to clarify, I actually need the sum (0.48395648).

I tried to do some for loops to add them but they got really messy, I'll post just for the sake of it.

Sample code:

all_unis_dict = {}
for skus1, subdict1 in unsw_mid_year_dict.items():
    for skus2, subdict2 in unsw_mid_year_dict.items():
        for dates1, days1 in subdict1.items():
            for dates2, days2 in subdict2.items():
                pprint(days1 + days2)

Anyone know a elegant way to do it?

解决方案

Seems like a use-case for a defaultdict of a defaultdict of int ...

The basic idea is that when you come across a key that isn't in the top-level defaultdict, you add the key (with an associated defaultdict(int) to store the date -> integer map as the value). When you come across a date which isn't in the nested dict, the defaultdict(int) will add the date with a default value of 0 (since int() called with no arguments returns 0).

Here's some code to make it concrete:

from collections import defaultdict
output = defaultdict(lambda: defaultdict(int))
for d in (dict1, dict2):
    for key, values_dict in d.items():
        for date, integer in values_dict.items():
            output[key][date] += integer

If you really want to be thorough, after the fact you can set the default_factory to None to prevent any more "default" behavior from the defaultdict:

output.default_factory = None
for default_date_dict in output.values():
    default_date_dict.default_factory = None

这篇关于将具有嵌套字典的两个字典合并成新的,将相同的键相加,并在Python中保留未更改的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆