在 python 中解决了一个套路,如果我输入 if name == main 则不起作用。什么在改变?

Solved a Kata in python, doesn't work if I put if name == main. What is changing?

为什么它给我 IndexError: list index out of range 第 28 行 if (cells[y][x] % TOP == 0

如果我删除 if __name__ == '__main__': 一切正常。

Count Connectivity Components 是 codewars 的套路,如果有兴趣的话。

TOP, RIGHT, BOTTOM, LEFT = 2, 3, 5, 7

def components(grid):
    grid = grid.split('\n')
    result = bfs(set_directions(grid))
    d = {}
    for k in result:
        if not k in d:
            d[k] = 0
        d[k] += 1
    result = []
    for k in d:
        result.append((k, d[k]))
    result.sort(reverse=True)
    return result

def bfs(cells):
    queue = [(0,0)]
    marked = set()
    result = []
    node_count = 0
    
    while True:
        x,y = queue.pop(0)
        marked.add((x,y))
        node_count += 1
        
        if (cells[y][x] % TOP == 0 
                and (x,y-1) not in marked 
                and (x,y-1) not in queue):
            queue.append((x,y-1))

        if (cells[y][x] % BOTTOM == 0 
                and (x,y+1) not in marked 
                and (x,y+1) not in queue):
            queue.append((x,y+1))

        if (cells[y][x] % LEFT == 0 
                and (x-1,y) not in marked 
                and (x-1,y) not in queue):
            queue.append((x-1,y))

        if (cells[y][x] % RIGHT == 0 
                and (x+1,y) not in marked 
                and (x+1,y) not in queue):
            queue.append((x+1,y))

        cells[y][x] = 'm'
        if not queue:
            result.append(node_count)
            node_count = 0
            flag = False
            for y in range(len(cells)):
                for x in range(len(cells[y])):
                    if cells[y][x] != 'm':
                        queue.append((x,y))
                        flag = True
                        break
                if flag:
                    break
            else:
                break
    return result

def set_directions(grid):
    cells = \
        [[1 for i in range(grid[0].count('+')-1)] 
                for r in range(len(grid)//2)]
    for r in range(1,len(grid), 2):
        for i in range(0, len(grid[r])-1, 3):
            subject = grid[r][i:i+3]
            if subject == '   ':
                cells[r//2][i//3] *= LEFT
        for i in range(3, len(grid[r]), 3):
            subject = grid[r][i-2:i+1]
            if subject == '   ':
                cells[r//2][i//3-1] *= RIGHT
    for c in range(1,len(grid[0]), 3):
        column = ''.join([r[c] for r in grid])
        for i in range(0,len(grid)-1, 2):
            subject = column[i:i+2]
            if subject == '  ':
                cells[i//2][c//3] *= TOP
        for i in range(1,len(grid),2):
            subject = column[i:i+2]
            if subject == '  ':
                cells[i//2][c//3] *= BOTTOM
    return cells

if __name__ == '__main__':
    a = '''\
    +--+--+--+
    |     |  |
    +--+  +  +
    |  |     |
    +  +--+--+
    |  |     |
    +--+--+--+'''
    print(a)
    print(components(a))

经过一些测试后,我注意到 a 字符串在两种情况下都不相同(使用 if __name__ == "__main__" 和不使用它)。问题是,当您使用 if __name__ == "__main__" 时,您会 缩进 a 字符串一次以将其放入 if 语句中。看起来你缩进了代码块,但实际上你将 spaces 放在字符串本身中。

if __name__ == "__main__":

['    +--+--+--+', '    |     |  |', '    +--+  +  +', '    |  |     |', '    +  +--+--+', '    |  |     |', '    +--+--+--+']

没有 if __name__ == "__main__":

['+--+--+--+', '|     |  |', '+--+  +  +', '|  |     |', '+  +--+--+', '|  |     |', '+--+--+--+']

要解决此问题,只需像这样删除空格:

if __name__ == '__main__':
    a = '''\
+--+--+--+
|     |  |
+--+  +  +
|  |     |
+  +--+--+
|  |     |
+--+--+--+'''
    print(components(a))