我是新來的stackoverflow。我搜索了適合我的問題的主題,但不幸的是我沒有找到一個。所以我開了一個新話題。創建矩陣
我必須在python中實現一個函數,該函數創建出某個輸入的關聯矩陣。我的問題是,我不太明白如何訪問indeces,所以每列只有一個'1'而不是兩個..:/
希望你們可以幫助我.. kinda丟失我的介意這一個
class incidence_matrix:
def __init__(self, vertices, edges):
self.vertices = vertices
self.edges = edges
self.liste = [[0 for i in range(vertices)] for i in range(vertices)]
#print(self.liste)
for i in range(0, vertices):
for j in range(0, len(edges)):
if edges[i][j-1] >= vertices or edges[i][j-1] < 0 or edges[i][j-1] >= vertices or edges[i][j-1] < 0:
print("Index out of range")
return
self.liste[edges[0][j+1]][edges[1][j+1]] = 1
self.liste[edges[1][j+1]][edges[0][j+1]] = 1
for x in range(0, vertices):
row = ""
for y in range(0, len(edges)):
row = row + str(self.liste[x][y]) + " "
print(row)
你能告訴邊緣字典樣子 – Navidad20
圖表= incidence_matrix(4,[(1,2),(0, 1),(0,2)]) –