读取网络的邻接矩阵,将其保存为连边形式

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Tue Dec  1 21:23:51 2020
 4 
 5 @author: chend
 6 """
 7 #读取邻接矩阵,将其保存为连边形式(文件格式为.txt)
 8 import numpy as np
 9 
10 A = np.loadtxt('./matrix.txt')
11 outf = open("edgelist.txt", "w")
12 for i in range(len(A)):
13     for j in range(len(A)):
14         if A[i][j] == 1:
15             outf.write(str(i+1)+" ")
16             outf.write(str(j+1)+"\n")
17             
18 outf.close()

txt文件结果展示:

 

posted on 2020-12-11 09:27  qjyyz  阅读(393)  评论(0编辑  收藏  举报