【发布时间】:2019-07-17 14:59:06
【问题描述】:
我不断收到 TypeError 'Range' 对象不支持项目分配。我尝试稍微更改代码,例如在范围之前添加 iter(...) 以及在范围之前添加 list(...)。但是,它没有帮助,错误仍在继续。 代码如下:
def findAnchor(anchors, node):
start = node
while node != anchors[node]:
node = anchors[node]
anchors[start] = node
return node
def unionFindConnectedComponents(graph):
anchors = range(len(graph))
for node in iter(range(len(graph))):
for neighbor in graph[node]:
if neighbor < node:
continue
a1 = findAnchor(anchors, node)
a2 = findAnchor(anchors, neighbor)
if a1 < a2:
anchors[a2] = a1
elif a2 < a1:
anchors[a1] = a2
labels = [None]*len(graph)
current_label = 0
for node in range(len(graph)):
a = findAnchor(anchors, node)
if a == node:
labels[a] = current_label
current_label += 1
else:
labels[node] = labels[a]
return anchors, labels
现在 TypeError 位于 anchors[start] = node 的开头。 node 是第二个函数的给定参数,它表示 iter(range(len(graph))) 中的 node。我用 iter 和 list 试过了,都不管用。怎么办?
【问题讨论】:
-
这么长时间后关闭,建议的欺骗目标是 100% 相同的......叹息
标签: python python-3.x