小编Mik*_*glu的帖子

范围对象不支持分配

我不断收到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 …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

1
推荐指数
1
解决办法
3213
查看次数

标签 统计

python ×1

python-3.x ×1