-2
EX_GRAPH1 = {0:[1,4,5],
1:[2,6],
2:[3],
3:[0],
4:[1],
5:[2],
6:[]
}
該函數採用一個有向圖有向圖 (表示爲字典),並計算 中度在圖中的節點。IndexError:列表索引超出範圍(它必須是字典,而不是列表)
def compute_in_degrees(digraph):
in_degrees = {}
i = 0
j = 0
matches = []
while i < len(digraph):
m = 0
while j < len(digraph):
if digraph[i][j] == i: <--- HERE IndexError: list index out of range
j += 1
m += 1
matches.append(m)
else:
j += 1
in_degrees[i] = matches
i += 1
return in_degrees
print compute_in_degrees(EX_GRAPH1)
幫助,請
這應該是'while j
jonrsharpe
2014-08-28 14:54:23