python - 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
解决方案
您当前的代码正在将生成器表达式附加到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
值使用三个不同的测试)。
推荐阅读
- python - 在 Python 中遍历 datetime.datetime 数组?
- birt - 多次请求 BIRT XML 数据源 url
- postman - Linkedin api v2 获取技能:'没有足够的权限访问 GET / 技能'
- sql - SQL - 使用日期表计算员工最后工作的天数?
- r - 为什么它不允许我在 R 中的 ggplot2 上的图表中添加图例?
- celery - 使用数据库后端时如何使芹菜过期结果
- typo3 - Typ3 后端中的 TCA 图标叠加层
- javascript - 如何使用 JavaScript 替换字符串中除第一个和最后一个字符之外的所有字符
- c++ - 如何通过 JTAG/调试器向 PC 发送数据
- javascript - 在 React Native 中正确显示我的徽标