简体   繁体   English

向节点,边和图添加属性

[英]Adding attributes to nodes,edges and graphs

I am a new user of python and networkx and I want to ask you that how to add attributes to graphs,nodes and edges in networkx ?When I try to add it there is an error Traceback (most recent call last): 我是python和networkx的新用户,我想问你如何向networkx图,节点和边添加属性?当我尝试添加它时,出现Traceback错误(最近一次调用是最近一次):

File "<pyshell#9>", line 1, in <module>
    G[1][3]['color']='blue'
KeyError: 3

I have searched for attribute package for networkx but not found. 我已搜索networkx属性包, networkx找到。

Well it seems your node G[1] doesn't have four children (3 wuold be the fourth and 0 the first). 好吧,看来您的节点G[1]没有four孩子(第三个为wuold,第一个为0)。

According to the python documetation: 根据python文档:

exception KeyError 异常KeyError
Raised when a mapping (dictionary) key is not found in the set of existing keys. 在现有键集中找不到映射(字典)键时引发。

In other words G[1][3] does not exists. 换句话说, G[1][3]不存在。

If you are starting to Python, now is a good time to start to learn how to debug your code. 如果您开始使用Python,那么现在是开始学习如何调试代码的好时机。 That way you will get more tools for findout what's going on with your code. 这样,您将获得更多工具来查找代码中发生的情况。

You can start at: The Python Debugger 您可以从以下位置开始: Python调试器

or check this question: Python debugging tips 或查看以下问题: Python调试技巧

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

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