我有坐标 -200|200、400 高 400 长 0|0 的网格 x|y 在中间。当它找到某些东西并将坐标保存在列表 [] 或我可以过滤所有查找坐标的某个地方时,我需要遍历所有和每次。谢谢你的帮助 x = -200 y = -200 for yval in range(400): for xval in range(400): do something... x += 1 y += 1这不适用于所有网格
2 回答

qq_遁去的一_1
TA贡献1725条经验 获得超8个赞
做这样的事情。两个 for 循环分别从x_lowtox_high和y_lowto迭代y_high
#Limits for iterating through the grid
x_low = -200
x_high = 201
y_low = -200
y_high = 201
#List to append your coordinates to, if condition is true
coords = []
for xval in range(x_low, x_high):
for yval in range(y_low, y_high):
if condition:
coords.append((xval,yval))

BIG阳
TA贡献1859条经验 获得超6个赞
您对x和y变量的使用有点多余。您可以简单地更改迭代的起点,并拥有xval并yval表示您当前的坐标。
for xval in range(-200, 201):
for yval in range(-200, 201):
# xval and yval now represent your current x coordinate and y coordinate
在range(start, end)函数的这种用法中,您指定起点和终点,然后从[start, ..., end)
添加回答
举报
0/150
提交
取消