简体   繁体   English

从边列表中读取无向图

[英]Read an undirected graph from a list of edges

I am having problems with reading an undirected graph from a list of edges. 我在从边列表中读取无向图时遇到问题。 I have my list of edges in a txt file like this: 我在txt文件中有这样的边列表:

BND IEF 0.943176118
BND LQD 0.885572253
BND TIP 0.83072059
BND TLT 0.897231452
DBC USO 0.885015182

etc. 等等

And then my code is: 然后我的代码是:

G0 = nx.Graph()

G0 = nx.read_edgelist(place_holder + "edges_for_graph.txt", nodetype = str, data = (('weight', int),))

But when I run the code I have this problem: 但是当我运行代码时,我遇到了这个问题:

TypeError: Failed to convert weight data 0.943176118 to type <class 'int'>.

I have tried changing the txt file (with only one space between each value) but it is not working, Does anyone know how to fix it because the values are int. 我尝试过更改txt文件(每个值之间只有一个空格),但是它不起作用,有人会因为值是int而知道如何解决它。

Well, from the data snippet, your weights arn't ints. 好吧,从数据片段来看,您的权重并非整数。 You either convert them to int somehow or instead store them as float s (probably the second one is what you want to do as the values seem to be between 0 and 1). 您可以以某种方式将它们转换为int,或者将它们存储为float s(可能第二个是您想要做的,因为值似乎在0到1之间)。

For the first way you could preprocess your file to drop the weigths or transform them to values of 1. But to just read them correctly, you would use ('weight', float) instead of ('weight', int) as in the example here . 对于第一种方式,您可以预处理文件以删除weigth或将其转换为值1。但是,为了正确读取它们,可以使用('weight', float)代替('weight', int)这里的例子。

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

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