Search code examples
pythondatagrid

Python loop thought a grid -200|200


I have grid x|y with coordinates -200|200, 400 height 400 long 0|0 in the middle. I need loop through all and every time when it finds something and it saves coordinates in a list[] or somewhere where I can filter all find coordinates.

thanks for any help

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

this not work for all grid


Solution

  • Do something like this. Both for loops iterate from x_low to x_high and y_low to y_high respectively

    #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))