简体   繁体   English

如何将python字典转换为图形对象NetworkX

[英]how to convert a python dictionary into a graph object, NetworkX

I'm new to python, and I am trying to convert a dictionary into a graph object in Networkx. 我是python的新手,正在尝试将字典转换为Networkx中的图形对象。 The dictionary: 词典:

dict = {'key1': ['value1', 'value2', 'value3'], 'key2': ['value1', 'value2']}

I want the keys to be an ego node, and values to be their alter nodes (with out-degree going from an ego to an alter). 我希望键是一个自我节点,而值则是它们的变更节点(出自从自我到变更的度数)。 Is this possible to achieve in Python?. 这可以在Python中实现吗?

You can iterate over keys just by using the dictionary as an iterable: 您可以仅通过将字典用作可迭代对象来对键进行迭代:

for key in d:
    #...

and, as you probably already know, you access values in the dictionary using the key as "index": 并且,您可能已经知道,您可以使用键作为“索引”来访问字典中的值:

d[key]

Iterating over lists (which your dictionary values are) is similar to iterating over the keys of the dictionary. 遍历列表(字典值是该列表)类似于遍历字典的键。

Combining these pieces of knowledge with the excellent networkx example on creating graphs should be straightforward, so you should try it, and ask a new question if you have any specific problem 将这些知识与出色的networkx示例创建图表结合起来应该很简单,因此您应该尝试一下,并询问是否有任何特定问题的新问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM