簡體   English   中英

python:詞典到詞典的元組

[英]python: tuple of dictionary to Dictionary

如何轉換字典元組,例如下面的示例:

({(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})

像字典一樣簡單的形式:

{(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}

只需迭代元組並使用字典理解重建字典“flat”:

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)

結果:

{(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}

你不能使用dict merge comprehension(但),但你可以通過鏈圖

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

注意: collections.ChainMap是Python 3.3中的新增功能。

它實際上是collections.Mapping的子類,因此根據用例,您甚至可能不需要轉換回普通字典。

如果所需dict元素的順序很重要並且需要按照問題中的dict進行排序,請使用collections.OrderedDict

# `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)])

但返回排序的dict維持與問題中所需的順序相同的順序為:

{(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}

但是如果所需dict中的元素順序無關緊要 ,您可以使用簡單的字典理解來實現它:

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

其中required_dict的值為:

{
    (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
}

注意:所需字典中的項目順序不同,因為Python中的字典本質上是無序的。

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

您可以使用元組中的所有dicts更新初始dict:

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)

另一個,Python 3.5及更新版專用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM