【问题标题】:Combine two dictionaries, concatenate string values?结合两个字典,连接字符串值?
【发布时间】:2013-07-10 09:37:57
【问题描述】:

相关:Is there any pythonic way to combine two dicts (adding values for keys that appear in both)?

我想合并两个 string:string 字典,并连接这些值。上面的帖子推荐使用collections.Counter,但它不处理字符串连接。

>>> from collections import Counter
>>> a = Counter({'foo':'bar', 'baz':'bazbaz'})
>>> b = Counter({'foo':'baz'})
>>> a + b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py", line 569, in __add__
TypeError: cannot concatenate 'str' and 'int' objects

(我的猜测是 Counter 尝试将 b['baz'] 设置为 0。)

我想得到{'foo':'barbaz', 'baz':'bazbaz'} 的结果。串联顺序对我来说并不重要。什么是干净的 Pythonic 方式来做到这一点?

【问题讨论】:

  • 如果第二个字典看起来像:{'foo':'baz','spam':'eggs'}?
  • @AshwiniChaudhary {'foo':'barbaz', 'baz':'bazbaz', 'spam':'eggs'}

标签: python string dictionary


【解决方案1】:

字典理解:

>>> d = {'foo': 'bar', 'baz': 'bazbaz'}
>>> d1 = {'foo': 'baz'}
>>> keys = d.viewkeys() | d1.viewkeys()
>>> {k : d.get(k, '') + d1.get(k, '') for k in keys}
{'foo': 'barbaz', 'baz': 'bazbaz'}

对于 Python 2.6 及更早版本:

>>> dict((k, d.get(k, '') + d1.get(k, '')) for k in keys)
{'foo': 'barbaz', 'baz': 'bazbaz'}

这适用于任意数量的字典:

def func(*dicts):
    keys = set().union(*dicts)
    return {k: "".join(dic.get(k, '') for dic in dicts)  for k in keys}
... 
>>> d = {'foo': 'bar', 'baz': 'bazbaz'}
>>> d1 = {'foo': 'baz','spam': 'eggs'}
>>> d2 = {'foo': 'foofoo', 'spam': 'bar'}
>>> func(d, d1, d2)
{'foo': 'barbazfoofoo', 'baz': 'bazbaz', 'spam': 'eggsbar'}

【讨论】:

    【解决方案2】:

    可以写一个通用的helper,比如:

    a = {'foo':'bar', 'baz':'bazbaz'}
    b = {'foo':'baz'}
    
    def concatd(*dicts):
        if not dicts:
            return {} # or should this be None or an exception?
        fst = dicts[0]
        return {k: ''.join(d.get(k, '') for d in dicts) for k in fst}
    
    print concatd(a, b)
    # {'foo': 'barbaz', 'baz': 'bazbaz'}
    
    c = {'foo': '**not more foo!**'}
    print concatd(a, b, c)
    # {'foo': 'barbaz**not more foo!**', 'baz': 'bazbaz'}
    

    【讨论】:

      【解决方案3】:

      可以使用defaultdict 来实现:

      from collections import defaultdict
      
      a = {'foo': 'bar', 'baz': 'bazbaz'}
      b = {'foo': 'baz'}
      
      new_dict = defaultdict(str)
      for key, value in a.items():
          new_dict[key] += value
      for key, value in b.items():
          new_dict[key] += value
      
      print(new_dict)
      # defaultdict(<class 'str'>, {'foo': 'barbaz', 'baz': 'bazbaz'})
      print(dict(new_dict))
      # {'foo': 'barbaz', 'baz': 'bazbaz'}
      

      如果要加入的字典很多,我们可以使用itertools.chain.from_iterable:

      from collections import defaultdict
      from itertools import chain
      
      a = {'foo': 'bar', 'baz': 'bazbaz'}
      b = {'foo': 'baz'}
      c = {'baz': '123'}
      dicts = [a, b, c]
      
      new_dict = defaultdict(str)
      for key, value in chain.from_iterable(map(dict.items, dicts)):
          new_dict[key] += value
      
      print(dict(new_dict))
      # {'foo': 'barbaz', 'baz': 'bazbaz123'}
      

      【讨论】:

        猜你喜欢
        • 2013-08-08
        • 2022-11-10
        • 2013-11-05
        • 2019-01-09
        • 2011-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多