首页 > 解决方案 > Python将对象添加到for循环迭代中的每个项目

问题描述

前几天开始学习python,其实对它的能力和语法灵活性印象非常深刻,但是今天遇到了一个在其他编程语言中从未见过的奇怪bug,我想是我的对 Python 的了解有限,我将不胜感激对此类行为的任何帮助和解释。

我有一个简单的 for 循环,在其中迭代节点列表,在每次迭代中,我将邻居添加到当前节点,但似乎它们不仅添加到当前节点,还添加到集合中的每个其他节点,所以在最后而不是拥有最多 8 个邻居的节点我最终拥有具有(8 * 集合中的节点数)邻居的节点,我不知道我在这里错过了什么。

def evaluate_neighbours(nodes):
for node in nodes:
    node.neighbours.append(n for n in nodes if n.x == node.x - 1 and n.y == node.y)
    node.neighbours.append(n for n in nodes if n.x == node.x + 1 and n.y == node.y)
    node.neighbours.append(n for n in nodes if n.y == node.y - 1 and n.x == node.x)
    node.neighbours.append(n for n in nodes if n.y == node.y + 1 and n.x == node.x)
    node.neighbours.append(n for n in nodes if n.y == node.y + 1 and n.x == node.x + 1)
    node.neighbours.append(n for n in nodes if n.y == node.y + 1 and n.x == node.x - 1)
    node.neighbours.append(n for n in nodes if n.x == node.x - 1 and n.y == node.y + 1)
    node.neighbours.append(n for n in nodes if n.x == node.x + 1 and n.y == node.y - 1)

编辑:

生成节点的节点类和代码如下:

class Node:
x = 0
y = 0
neighbours = []
alive = False

def __init__(self, _x, _y, _alive):
    self.x = _x
    self.y = _y
    self.alive = _alive


def generate_grid(data):
nodes = []
for index_y, y in enumerate(data):
    for index_x, x in enumerate(y):
        if x == "X":
            nodes.append(Node(index_x, index_y, True))
        else:
            nodes.append(Node(index_x, index_y, False))
return nodes

标签: pythonfor-loop

解决方案


您当前的代码正在将生成器表达式附加到neighbors列表中。我很确定您想要附加实际节点,而不是生成器。此外,由于生成器是闭包(如果您不知道这意味着什么,请不要太担心),在决定要附加哪些节点时,您的计算可能是错误的。

我建议创建第二个显式循环,而不是使用任何生成器表达式,并将生成器表达式中if的所有子句转换为一条if语句中条件的一部分。看起来像这样:

for node in nodes:
    for n in nodes:
        if (n.x == node.x - 1 and n.y == node.y or
            n.x == node.x + 1 and n.y == node.y or
            ...):
                 node.neighbours.append(n)

我没有复制所有条件,但是您可以这样做,只需将它们与or. 如果您想简化事情,您也许可以对一些条件进行分组(例如,您可以测试n.x == node.x - 1 and node.y - 1 <= n.y <= node.y + 1而不是对不同的y值使用三个不同的测试)。


推荐阅读