简体   繁体   English

模块“ networkx”没有属性“ add_nodes_from”

[英]module 'networkx' has no attribute 'add_nodes_from'

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'

Here is a sample code: 这是一个示例代码:

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

Environment is pip managed python 3.7.2 with networkx 2.2, MacOS 10.13.6 环境是由pip管理的python 3.7.2和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 这是我尝试遵循的参考: https : //networkx.github.io/documentation/stable/reference/classes/generation/networkx.Graph.add_nodes_from.html#networkx.Graph.add_nodes_from

Thank you 谢谢

You're calling add_nodes_from the wrong way. 您从错误的方式调用add_nodes_from It is a method of the base MultiGraph class, and not an attribute of the networkx module itself. 它是MultiGraph基类的方法,而不是networkx模块本身的属性。 So the Syntax should be 所以语法应该是

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

(notice the dot instead of '='). (注意点而不是'=')。

So i guess you're calling it as 所以我想你把它称为

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. 在您的主代码中,这又是错误的语法-请查看此处或您自己发布的链接以获取更多信息。

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

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