Combine two dictionaries, concatenate string values?

前端 未结 3 1542
感情败类
感情败类 2020-12-15 01:45

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

I\'d like to merge two string:string dictionaries, and concaten

相关标签:
3条回答
  • 2020-12-15 02:04

    Dict-comprehension:

    >>> 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'}
    

    For Python 2.6 and earlier:

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

    This will work for any number of dicts:

    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'}
    
    0 讨论(0)
  • 2020-12-15 02:15

    One can use defaultdict to achieve this:

    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'}
    

    If there are many dicts to join, we could use 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'}
    
    0 讨论(0)
  • 2020-12-15 02:16

    Can write a generic helper, such as:

    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'}
    
    0 讨论(0)
提交回复
热议问题