【问题标题】:Merging dictionaries function with for/in statement使用 for/in 语句合并字典功能
【发布时间】:2020-05-19 18:35:12
【问题描述】:

我是 Python 的初学者,所以我需要一个简单的解释。

我有两本词典:

supply_dictionarie = {'Apple': 20, 'Cigar': 100, 'Milk': 210, 'Flower': 75}

goods_dictionarie = {'Milk': 210, 'Apple': 50, 'WhiteWine': 200, 'Beer': 300, 'Oranges': 400}

我正在尝试合并这两个字典,同时对重复键的值求和。我的意图是获得:

merged_dictionarie = {'Milk': 420, 'Cigar': 100, 'Apple': 70, 'WhiteWine': 200, 'Beer': 300, 'Oranges': 400, 'Flower': 75} *(not necessarily in this order)

我正在使用以下代码:

def merge_supply():
    for k in supply_dictionarie:
        if k in set(goods_dictionarie):
            merged_dictionarie[k] = goods_dictionarie.get(k, 0) + supply_dictionarie.get(k, 0)
        elif k not in set(goods_dictionarie):
            merged_dictionarie[k] = supply_dictionarie.get(k, 0)
        else:
            break
    for k in goods_dictionarie:
         if k not in set(supply_dictionarie):
            merged_dictionarie[k] = goods_dictionarie.get(k, 0)
         else:
            break

但我得到的只是:

merged_dictionarie = {'Apple': 70, 'Cigar': 100, 'Milk': 420, 'Flower': 75}

对我来说,这表明第二个“for/in”表达式没有成功执行工作。出了什么问题?

【问题讨论】:

    标签: python dictionary merge


    【解决方案1】:

    你可以使用这个来简化你的循环:

    >>> from collections import defaultdict
    >>> merged = defaultdict(int)
    >>> for i in supply_dictionarie :
    ...     merged[i] += supply_dictionarie[i]
    ... 
    >>> for i in goods_dictionarie :
    ...     merged[i] += goods_dictionarie[i]
    ... 
    >>> dict(merged)
    {'Beer': 300, 'Flower': 75, 'Apple': 70, 'Cigar': 100, 'WhiteWine': 200, 'Oranges': 400, 'Milk': 420}
    

    【讨论】:

      【解决方案2】:

      您可以使用collections.Counter

      > $ ipython3                                                                                                                 
      Python 3.8.2 (default, Apr  8 2020, 14:31:25) 
      Type 'copyright', 'credits' or 'license' for more information
      IPython 7.14.0 -- An enhanced Interactive Python. Type '?' for help.
      
      In [1]: from collections import Counter                                                                                       
      
      In [2]: supply_dictionarie = Counter({'Apple': 20, 'Cigar': 100, 'Milk': 210, 'Flower': 75})                                  
      
      In [3]: goods_dictionarie = Counter({'Milk': 210, 'Apple': 50, 'WhiteWine': 200, 'Beer': 300, 'Oranges': 400})                
      
      In [4]: dict(supply_dictionarie + goods_dictionarie)                                                                          
      Out[4]: 
      {'Apple': 70,
       'Cigar': 100,
       'Milk': 420,
       'Flower': 75,
       'WhiteWine': 200,
       'Beer': 300,
       'Oranges': 400}
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-21
        • 2011-11-20
        • 1970-01-01
        • 2019-12-02
        • 1970-01-01
        • 1970-01-01
        • 2022-09-29
        • 1970-01-01
        相关资源
        最近更新 更多