-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