简体   繁体   English

Python - 类型错误:function 占用 3 个位置 arguments 但给出了 4 个

[英]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.

相关问题 Python TypeError:接受 4 个位置参数,但给出了 5 个 - Python TypeError: takes 4 positional arguments but 5 were given TypeError: with_column() 从 3 到 4 个位置 arguments 但给出了 5 个(python) - TypeError: with_column() takes from 3 to 4 positional arguments but 5 were given (python) Python: TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given - Python : TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given Python TypeError:衍生物_circ()接受2个位置参数,但给出了6个 - Python TypeError: derivatives_circ() takes 2 positional arguments but 6 were given TypeError:__init __()接受2个位置参数,但是给了3个Python 3? - TypeError: __init__() takes 2 positional arguments but 3 were given Python 3? 类型错误:update() 需要 2 个位置参数,但给出了 3 个:Python - TypeError: update() takes 2 positional arguments but 3 were given : Python Python - TypeError: generateID() 需要 3 个位置 arguments 但给出了 4 个 - Python - TypeError: generateID() takes 3 positional arguments but 4 were given Python - 类型错误:__init__() 需要 3 个位置 arguments 但给出了 4 个 - Python - TypeError: __init__() takes 3 positional arguments but 4 were given python super:TypeError:__init __()接受2个位置参数,但给出了3个 - python super :TypeError: __init__() takes 2 positional arguments but 3 were given Python/MySQL TypeError:execute() 需要 2 到 4 个位置参数,但给出了 5 个 - Python/MySQL TypeError: execute() takes from 2 to 4 positional arguments but 5 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM