python: tuple of dictionary to Dictionary

2020-04-11 06:42发布

How can I convert tuple of dictionaries like example present below:

({(1, 2): 3},
 {(1, 3): 5},
 {(1, 4): 5},
 {(2, 4): 5},
 {(1, 5): 10},
 {(2, 6): 9},
 {(1, 6): 9},
 {(2, 1): 2},
 {(2, 2): 3},
 {(2, 3): 5},
 {(2, 5): 10},
 {(1, 1): 2})

to a rather simpler form like dictionary:

{(1, 1): 2,
 (1, 2): 3,
 (1, 3): 5,
 (1, 4): 5,
 (1, 5): 10,
 (1, 6): 9,
 (2, 1): 12,
 (2, 2): 7,
 (2, 3): 7,
 (2, 4): 3,
 (2, 5): 4,
 (2, 6): 2}

6条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-04-11 06:56

You can't use a dict merge comprehension (yet), but you can go via a chain map:

>>> from collections import ChainMap
>>> dict(ChainMap(*dicts))
{(1, 1): 2,
 (1, 2): 3,
 (1, 3): 5,
 (1, 4): 5,
 (1, 5): 10,
 (1, 6): 9,
 (2, 1): 2,
 (2, 2): 3,
 (2, 3): 5,
 (2, 4): 5,
 (2, 5): 10,
 (2, 6): 9}

Note: collections.ChainMap is new in Python 3.3.

It's actually a subclass of collections.Mapping, so depending on the use-case you might not even need to convert back to a plain dict.

查看更多
\"骚年 ilove
3楼-- · 2020-04-11 06:58

If order of the elements in the desired dict matters and is needed to be sorted as mentioned in the question, use collections.OrderedDict as:

# `original_list` is the variable holding the
# `list` of `dict` as mentioned in the question

required_dict = OrderedDict(
    sorted((k, v) for sub_list in original_list for k, v in sub_list.items()))

# `OrderedDict` is represented as:
#    OrderedDict([((1, 1), 2), ((1, 2), 3), ((1, 3), 5), ((1, 4), 5), ((1, 5), 10), ((1, 6), 9), ((2, 1), 2), ((2, 2), 3), ((2, 3), 5), ((2, 4), 5), ((2, 5), 10), ((2, 6), 9)])

but returns sorted dict maintaining the order equivalent to the one desired in the question as:

{(1, 1): 2,
 (1, 2): 3,
 (1, 3): 5,
 (1, 4): 5,
 (1, 5): 10,
 (1, 6): 9,
 (2, 1): 12,
 (2, 2): 7,
 (2, 3): 7,
 (2, 4): 3,
 (2, 5): 4,
 (2, 6): 2}

But if order of elements in the desired dict doesn't matter, you may use simple dict comprehension to achieve it as:

required_dict = {k: v for sub_list in original_list for k, v in sub_list.items()}

where the value of required_dict will be:

{
    (1, 2): 3, 
    (2, 6): 9, 
    (1, 4): 5, 
    (1, 1): 2, 
    (1, 5): 10, 
    (1, 3): 5, 
    (1, 6): 9, 
    (2, 1): 2, 
    (2, 2): 3, 
    (2, 3): 5, 
    (2, 5): 10, 
    (2, 4): 5
}

Note: Order of items in the desired dict are different because dictionaries in Python are unordered by nature.

查看更多
时光不老,我们不散
4楼-- · 2020-04-11 07:02
>>> a=({(1, 2): 3},
 {(1, 3): 5},
 {(1, 4): 5},
 {(2, 4): 5},
 {(1, 5): 10},
 {(2, 6): 9},
 {(1, 6): 9},
 {(2, 1): 2},
 {(2, 2): 3},
 {(2, 3): 5},
 {(2, 5): 10},
 {(1, 1): 2})
>>> {key: x[key] for x in a for key in x}
{(1, 2): 3, (2, 6): 9, (1, 4): 5, (1, 1): 2, (1, 5): 10, (1, 3): 5, (1, 6): 9, (2, 1): 2, (2, 2): 3, (2, 3): 5, (2, 5): 10, (2, 4): 5}
>>> 
查看更多
你好瞎i
5楼-- · 2020-04-11 07:02

You can update an initial dict with all the dicts form the tuple:

values = ({(1, 2): 3},
 {(1, 3): 5},
 {(1, 4): 5},
 {(2, 4): 5},
 {(1, 5): 10},
 {(2, 6): 9},
 {(1, 6): 9},
 {(2, 1): 2},
 {(2, 2): 3},
 {(2, 3): 5},
 {(2, 5): 10},
 {(1, 1): 2})

d = dict()
reduce(lambda _, v: d.update(v), values)
查看更多
Evening l夕情丶
6楼-- · 2020-04-11 07:09

just iterate on the tuples and rebuild the dictionary "flat" using a dictionary comprehension:

a = ({(1, 2): 3},
 {(1, 3): 5},
 {(1, 4): 5},
 {(2, 4): 5},
 {(1, 5): 10},
 {(2, 6): 9},
 {(1, 6): 9},
 {(2, 1): 2},
 {(2, 2): 3},
 {(2, 3): 5},
 {(2, 5): 10},
 {(1, 1): 2})

b = {k:v for t in a for k,v in t.items()}

print(b)

result:

{(1, 2): 3, (2, 6): 9, (2, 1): 2, (1, 1): 2, (1, 5): 10, (1, 3): 5, (1, 6): 9, (1, 4): 5, (2, 2): 3, (2, 3): 5, (2, 5): 10, (2, 4): 5}
查看更多
够拽才男人
7楼-- · 2020-04-11 07:16

Another one, exclusive to Python 3.5 and newer:

>>> functools.reduce(lambda d1, d2: {**d1, **d2}, values)
{(1, 2): 3, (2, 6): 9, (2, 1): 2, (1, 1): 2, (1, 5): 10, (1, 3): 5, (1, 6): 9, (1, 4): 5, (2, 2): 3, (2, 3): 5, (2, 5): 10, (2, 4): 5}
查看更多
登录 后发表回答