gpt4 book ai didi

python - 向节点、边和图添加属性

转载 作者:太空宇宙 更新时间:2023-11-03 18:21:19 30 4
gpt4 key购买 nike

我是 python 和 networkx 的新用户,我想问你如何向 networkx 中的图、节点和边添加属性?当我尝试添加它有一个错误回溯(最近一次调用最后):

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

我已搜索networkx的属性包,但没有找到。

最佳答案

看来你的节点G[1]没有四个子节点(3是第四个,0是第一个)。

根据Python文档:

exception KeyError
Raised when a mapping (dictionary) key is not found in the set of existing keys.

换句话说,G[1][3] 不存在。

如果您开始使用 Python,现在是开始学习如何调试代码的好时机。这样您将获得更多工具来了解代码的情况。

您可以从:The Python Debugger开始

或者检查这个问题:Python debugging tips

关于python - 向节点、边和图添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24087207/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com