Python - 类型错误:函数需要 3 个位置参数,但给出了 4 个

Nic*_*que -3 python networkx

我正在尝试在城市和字典之间添加边缘以及它们之间的距离。当我尝试编译代码时出现此错误,有人可以帮助我吗?

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})
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Joe*_*oel 5

我相信你的代码可以在 networkx 2.0 中工作(它似乎对我有用),但不能在 networkx 1.11 中工作。

在阅读 networkx 1.11 的文档时,您似乎需要执行以下任一操作

cities.add_edge('Boston', 'Nashville', distance=0.4})
Run Code Online (Sandbox Code Playgroud)

或者

cities.add_edge('Boston', 'Nashville', attr_dict = {'distance':0.4})
Run Code Online (Sandbox Code Playgroud)

但是我不能在我的机器上轻松测试它有 v2.0。