Python循环以为网格-200 | 200

时间:2019-04-09 18:53:05

标签: python datagrid

我的网格x | y的坐标为-200 | 200,中间为400高度400长0 | 0。每当发现某物并将坐标保存在list []或可以过滤所有找到的坐标的地方时,我就需要遍历所有对象。

感谢您的帮助

    x = -200
    y = -200
    for yval in range(400):
        for xval in range(400):
            do something...
            x += 1
        y += 1

这不适用于所有网格

2 个答案:

答案 0 :(得分:0)

您对xy变量的使用有点多余。您只需更改迭代的起点,并用xvalyval代表当前坐标。

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)进行迭代

答案 1 :(得分:0)

做这样的事情。两个for循环分别从x_low迭代到x_high,从y_low迭代到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))