模块'networkx'没有属性'add_nodes_from'

问题描述 投票:0回答:1

我有一个字典,作者姓名字符串作为索引,出版物的数量作为关联值。当我尝试从中添加节点到新图时,我收到以下错误:

AttributeError: module 'networkx' has no attribute 'add_nodes_from'

这是一个示例代码:

import networkx as nx
auth_dict = {"albert": 1, "Barbie": 3, "Charlie": 8}
G = nx.MultiGraph()
G = nx.add_nodes_from(auth_dict)

环境是pip管理python 3.7.2与networkx 2.2,MacOS 10.13.6

这是我试图遵循的参考:https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.add_nodes_from.html#networkx.Graph.add_nodes_from

谢谢

python python-3.x networkx
1个回答
3
投票

你以错误的方式打电话给add_nodes_from。它是基础 MultiGraph类的方法,而不是networkx模块本身的属性。所以语法应该是

G = nx.MultiGraph()
G.add_nodes_from(auth_dict)

(注意点而不是'=')。

所以我猜你称之为

G = nx.add_nodes_from(foo)

在您的主要代码中,这又是错误的语法 - 请查看here或您自己发布的链接以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.