[英]Python - TypeError: function takes 3 positional arguments but 4 were given
I am trying to add edges between the cities and dictionary with the distance between them.我正在尝试在城市和字典之间添加边缘以及它们之间的距离。 When I try to compile the code I get to this error, can anyone help me?
当我尝试编译我遇到此错误的代码时,任何人都可以帮助我吗?
import networkx as nx
cities = nx.Graph()
cities.add_edge('San Diego','LA',{'distance':0.4})
cities.add_edge('NY','Nashville',{'distance':5.6})
cities.add_edge('Boston','DC',{'distance':0.8})
I believe your code will work in.networkx 2.0 (it seems to work for me), but not in.networkx 1.11.我相信您的代码可以在 .networkx 2.0 中运行(它似乎对我有用),但不能在 .networkx 1.11 中运行。
In reading through the documentation for.networkx 1.11, it looks like you need to do either在阅读 .networkx 1.11 的文档时,您似乎需要执行以下任一操作
cities.add_edge('Boston', 'Nashville', distance=0.4)
or或者
cities.add_edge('Boston', 'Nashville', attr_dict = {'distance':0.4})
But I can't easily test it on my machine which has v2.0.但是我不能轻易地在我有 v2.0 的机器上测试它。
If you want to use a dictionary for attributes then you can do it with @Joel second example如果你想为属性使用字典,那么你可以使用@Joel 第二个例子
cities.add_edge('Boston', 'Nashville', attr_dict = {'distance':0.4})
however in this case you will get 'attr_dict' as an attribute in which you will have your dictionary.但是在这种情况下,您将获得“attr_dict”作为您将在其中拥有字典的属性。 Like this.
像这样。
cities.edges(data=True)
will return将返回
EdgeDataView([('Boston', 'Nashville', {'attr_dict': {'distance': 0.4}})])
EdgeDataView([('波士顿', '纳什维尔', {'attr_dict': {'距离': 0.4}})])
A way to get only your dictionary in attributes is add_edges_from()
:一种在属性中只获取字典的方法是
add_edges_from()
:
cities.add_edges_from([('San Diego','LA',{'distance':0.4})])
cities.add_edges_from([('NY','Nashville',{'distance':5.6}),
('Boston','DC',{'distance':0.8})])
cities.edges(data=True)
will return将返回
EdgeDataView([('San Diego', 'LA', {'distance': 0.4}), ('NY', 'Nashville', {'distance': 5.6}), ('Boston', 'DC', {'distance': 0.8})])
EdgeDataView([('圣地亚哥', '洛杉矶', {'距离': 0.4}), ('纽约', '纳什维尔', {'距离': 5.6}), ('波士顿', '华盛顿', { “距离”:0.8})])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.