在python中合并3个dict() [英] Merging 3 dict()'s in python

查看:104
本文介绍了在python中合并3个dict()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有逻辑上合并多个字典的方法,如果它们之间有共同的字符串?即使这些常见的字符串在一个dict()和另一个key的值之间匹配?



我在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' ,15],'5':16,'9':17,'10':[18,19,20]}
finaldict = level1dict


我想得到的结果是

 #。update或.append或.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模块。

解决方案

这是连接组件子图的问题,如果要使用 networkx 。这是您的问题的解决方案

 >>>将networkx导入为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' 10':[18,19,20]}
>>> G = nx.Graph()
>>>>对于lvl级别:
为键值,在lvl.items()中的值:
key = int(key)
try:
为节点值:
G .add_edge(key,node)
除了TypeError:
G.add_edge(key,value)


>>>对于nx.connected_component_subgraphs(G)中的sg:
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()


Is there a method of logically merging multiple dictionaries if they have common strings between them? Even if these common strings match between values of one dict() to a key of another?

I see a lot of similar questions on SO but none that seem to address my specific issue of relating multiple keys in "lower level files" to those in higher keys/values(level1dict)

Say we have:

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

When I say logically I mean, in level1dict 1=1,3 and in level2dict 1=4 and 3=5,9 so overall (so far) 1 = 1,3,4,5,9 (sorting not important)

The result I would like to get to is

#.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]}

Answered: Thank you Ashwini Chaudhary and Abhijit for the networkx module.

解决方案

This is a problem of connected component subgraphs and can be best determined if you want to use networkx. Here is a solution to your problem

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

Here is how you visualize it

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

这篇关于在python中合并3个dict()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆