TypeError 'Range'オブジェクトを取得し続けることは、アイテムの割り当てをサポートしていません。私は範囲の前にiter(...)を追加するだけでなく、範囲の前にリスト(...)を追加するように少しコードを変更しようとしました。しかし、それは助けにならず、エラーは続く。ここ はコードです:Python Rangeオブジェクトは割り当てをサポートしていません
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はアンカーで始まり、[スタート] =ノードです。そして、nodeは、iter(range(len(graph)))内のnodeについて言う2番目の関数から与えられた引数です。私はiterとリストを使って試してみたが、どちらもうまくいきませんでした。何をすべきか?
[TypeError: 'range'オブジェクトは重複している可能性があります](https:// stackoverfl ow.com/questions/20484195/typeerror-range-object-does-not-support-item-assignment) – jdhao