arr = [[0] * 6 for row in range(6)] # 聲明矩陣arr data = [[1, 2], [2, 1], [2, 3], [2, 4], [4, 3], [4, 1]] # 圖各邊的起點值和終點值 for i in range(6): # 讀取圖的數據 # print(data[0]) for j in range(6): # 填入arr矩陣 tmpi = data[i][0] # tmpi爲起始頂點 tmpj = data[i][1] # tmpj爲終止頂點 arr[tmpi][tmpj] = 1 # 有邊的點填入1 print('有向圖矩陣:') for i in range(1, 6): for j in range(1, 6): print('[%d] ' % arr[i][j], end='') # 打印矩陣內容 print()