Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何以所需格式将加权networkx图形导出到边列表?_Python_Networkx_Edge List - Fatal编程技术网

Python 如何以所需格式将加权networkx图形导出到边列表?

Python 如何以所需格式将加权networkx图形导出到边列表?,python,networkx,edge-list,Python,Networkx,Edge List,我试图获取一个加权networkx图,并将其转换为edgelist.txt文件,其中每一行都采用三个空格分隔的数字的形式,这些数字指示开始节点、结束节点和相应的权重 下面是我为一个简单的七节点加权无向图所做的尝试: import networkx as nx import numpy as np A = np.matrix([[0,7,7,0,0],[7,0,6,0,0],[7,6,0,2,1],[0,0,2,0,4], [0,0,1,4,0]]) G = nx.from_numpy

我试图获取一个加权networkx图,并将其转换为edgelist.txt文件,其中每一行都采用三个空格分隔的数字的形式,这些数字指示开始节点、结束节点和相应的权重

下面是我为一个简单的七节点加权无向图所做的尝试:

 import networkx as nx
 import numpy as np
 A = np.matrix([[0,7,7,0,0],[7,0,6,0,0],[7,6,0,2,1],[0,0,2,0,4], 
 [0,0,1,4,0]])
 G = nx.from_numpy_matrix(A)
 nx.write_edgelist(G, "weighted_test_edgelist.txt", delimiter=' ')
将创建文本文件,其外观如下所示:

0 1 {'weight': 7}

0 2 {'weight': 7}

1 2 {'weight': 6}

2 3 {'weight': 2}

2 4 {'weight': 1}

3 4 {'weight': 4}
但是,我希望上面的内容显示为

0 1 7

0 2 7

1 2 6

2 3 2

2 4 1

3 4 4
尝试:

输出:

0 1 7
0 2 7
1 2 6
2 3 2
2 4 1
3 4 4
每份文件:

数据:bool或list,如果为False,则为可选,不写入边缘数据。如果 True写入边缘数据字典的字符串表示形式。。如果 提供了一个列表(或其他iterable),写下指定的键
在名单上

0 1 7
0 2 7
1 2 6
2 3 2
2 4 1
3 4 4