python 列表转图结构_Python读取网络(图)边列表数据进而转化为邻接矩阵
生活随笔
收集整理的這篇文章主要介紹了
python 列表转图结构_Python读取网络(图)边列表数据进而转化为邻接矩阵
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import networkx as nx
G = nx.Graph()
path = './edge_list.txt'
edge_list = []
node_set = set() #集合的特性就是元素不會重復(fù),互異的
with open(path, 'r') as f:
for line in f:
cols = line.strip().split(' ')
y1=int(cols[0])
y2=int(cols[1])
node_set.add(y1)
node_set.add(y2)
edge = (y1,y2) #元組代表一條邊
edge_list.append(edge)
G.add_nodes_from(range(1,len(node_set)+1)) #節(jié)點序號從1開始編號
G.add_edges_from(edge_list)
#A = nx.to_numpy_matrix(G)
A = nx.adjacency_matrix(G).todense()
print(A)
示例:
連邊數(shù)據(jù):注意連邊數(shù)據(jù)最后的空行一定要刪掉,否則會報錯
輸出的鄰接矩陣:
總結(jié)
以上是生活随笔為你收集整理的python 列表转图结构_Python读取网络(图)边列表数据进而转化为邻接矩阵的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qqexternal.exe是什么进程如
- 下一篇: SSDP Discovery Servi