问题描述
我有一本字典,以作者姓名字符串作为索引,以出版物的数量作为关联值.当我尝试将节点添加到新图中时,出现以下错误:
I have a dictionary with author name strings as indexes and the number of publications as the associated value. When I try adding nodes to a new graph from it, I get the following error:
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
Environment is pip managed python 3.7.2 with networkx 2.2, MacOS 10.13.6
This is the reference I tried to follow: https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.add_nodes_from.html#networkx.Graph.add_nodes_from
谢谢
推荐答案
您调用 add_nodes_from
的方式是错误的.它是基础 MultiGraph
类,而不是 networkx
模块本身的属性.所以语法应该是
You're calling add_nodes_from
the wrong way. It is a method of the base MultiGraph
class, and not an attribute of the networkx
module itself. So the Syntax should be
G = nx.MultiGraph()
G.add_nodes_from(auth_dict)
(注意点而不是 '=').
(notice the dot instead of '=').
所以我猜你是叫它
G = nx.add_nodes_from(foo)
在你的主代码中,这也是错误的语法 - 看看 此处 或您自己发布的链接以获取更多信息.
in your main code, which is, again, the wrong syntax - look here or at the link you posted yourself for more info.
这篇关于模块“networkx"没有属性“add_nodes_from"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!