将变量分配给列表中的元组

Assigning Variables to Tuples in a List

我目前正在使用一个函数来 return 元组列表(坐标)。我需要分配这些坐标变量,以便我可以在 for 循环中使用它们。

我的函数是:

new_connect = astar.get_path(n1x, n1y, n2x, n2y)

print(new_connect) 我得到输出:

[(76, 51), (75, 51), (74, 51), (73, 51), (72, 51), (71, 51), (70, 51), (69, 51), ...]

我需要分配这些元组变量,即 (x, y)
所以它们可以在下面的for循环中使用:

for x in range(new_connect):
    for y in range(new_connect):
        self.tiles[x][y].blocked = False
        self.tiles[x][y].block_sight = False

哪个(应该)绘制坐标并更改它们的图块值。

非常感谢任何帮助。我一直致力于此,感觉我错过了一些非常简单的东西。

可以使用解包

new_connect = [(76, 51), (75, 51), (74, 51), (73, 51), (72, 51), (71, 51), (70, 51), (69, 51)]
for x, y in new_connect:
    print(x, y)

因此,尚不清楚 range(new_connect) 的实际运作方式。它不应该。您应该收到 TypeError,因为列表对象不是 range.

的正确参数

也就是说,您应该能够通过在 for 语句本身中执行元组解包来为元组列表创建一个 for 循环。

for x, y in astar.get_path(...):
    ...