3

如果它们之间有共同的字符串,是否有一种逻辑合并多个字典的方法?即使这些公共字符串在一个 dict() 的值与另一个键的值之间匹配?

我在 SO 上看到了很多类似的问题,但似乎没有一个可以解决我将“较低级别文件”中的多个键与较高键/值中的多个键相关联的具体问题(level1dict)

假设我们有:

level1dict = { '1':[1,3], '2':2 }
level2dict = { '1':4, '3':[5,9], '2':10 }
level3dict = { '1':[6,8,11], '4':12, '2':13, '3':[14,15], '5':16, '9':17, '10':[18,19,20]}
finaldict = level1dict

当我从逻辑上说时,我的意思是,在 level1dict 1=1,3 和 level2dict 1=4 和 3=5,9 中(到目前为止)1 = 1,3,4,5,9(排序不重要)

我想得到的结果是

#.update or .append or .default?
finaldict = {'1':[1,3,4,5,9,6,8,11,12,14,15,16,17] '2':[2,10,18,19,20]}

回答:感谢 Ashwini Chaudhary 和 Abhijit 提供 networkx 模块。

4

3 回答 3

9

这是连接组件子图的问题,如果您想使用networkx可以最好地确定。这是您的问题的解决方案

>>> import networkx as nx
>>> level1dict = { '1':[1,3], '2':2 }
>>> level2dict = { '1':4, '3':[5,9], '2':10 }
>>> level3dict = { '1':[6,8,11], '4':12, '2':13, '3':[14,15], '5':16, '9':17, '10':[18,19,20]}
>>> G=nx.Graph()
>>> for lvl in level:
    for key, value in lvl.items():
        key = int(key)
        try:
            for node in value:
                G.add_edge(key, node)
        except TypeError:
            G.add_edge(key, value)


>>> for sg in nx.connected_component_subgraphs(G):
    print sg.nodes()


[1, 3, 4, 5, 6, 8, 9, 11, 12, 14, 15, 16, 17]
[2, 10, 13, 18, 19, 20]
>>> 

这是您如何可视化它的方式

>>> import matplotlib.pyplot as plt
>>> nx.draw(G)
>>> plt.show()

在此处输入图像描述

于 2012-12-05T08:10:21.083 回答
2

几点注意事项:

  1. 有些值是数字,有些是列表,这很不方便。首先尝试将数字转换为单项列表。
  2. 如果顺序不重要,最好使用sets 而不是列表。他们有各种“逻辑”操作的方法。

然后你可以这样做:

In [1]: dict1 = {'1': {1, 3}, '2': {2}}

In [2]: dict2 = {'1': {4}, '2': {10}, '3': {5, 9}}

In [3]: dict3 = {'1': {6, 8, 11}, '2': {13}, '4': {12}}

In [4]: {k: set.union(*(d[k] for d in (dict1, dict2, dict3)))
    for k in set.intersection(*(set(d.keys()) for d in (dict1, dict2, dict3)))}
Out[4]: {'1': set([1, 3, 4, 6, 8, 11]), '2': set([2, 10, 13])}
于 2012-12-05T08:02:12.220 回答
2
In [106]: level1dict = { '1':[1,3], '2':2 }

In [107]: level2dict = { '1':4, '3':[5,9], '2':10 }
In [108]: level3dict = { '1':[6,8,11], '4':12, '2':13, '3':[14,15], '5':16, '9':17, '10':[18,19,20]}

In [109]: keys=set(level2dict) & set(level1dict) & set(level3dict) #returns ['1','2']
In [110]: dic={}

In [111]: for key in keys:
    dic[key]=[]
    for x in (level1dict,level2dict,level3dict):
        if isinstance(x[key],int):
            dic[key].append(x[key])
        elif isinstance(x[key],list):
            dic[key].extend(x[key])
   .....:             

In [112]: dic
Out[112]: {'1': [1, 3, 4, 6, 8, 11], '2': [2, 10, 13]}

# now iterate over `dic` again to get the values related to the items present
# in the keys `'1'` and `'2'`.

In [122]: for x in dic:
    for y in dic[x]:
        for z in (level1dict,level2dict,level3dict):
            if str(y) in z and str(y) not in dic:
                if isinstance(z[str(y)],(int,str)):
                     dic[x].append(z[str(y)])
                elif isinstance(z[str(y)],list):
                     dic[x].extend(z[str(y)])
   .....:                     

In [123]: dic
Out[123]: 
{'1': [1, 3, 4, 6, 8, 11, 5, 9, 14, 15, 12, 16, 17],
 '2': [2, 10, 13, 18, 19, 20]}
于 2012-12-05T08:18:22.410 回答