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)+" ") 17 18 outf.close()
txt文件结果展示: