做这样的事情。两个 for 循环分别从x_lowtox_high和y_lowto迭代y_high#Limits for iterating through the gridx_low = -200x_high = 201y_low = -200y_high = 201#List to append your coordinates to, if condition is truecoords = []for xval in range(x_low, x_high): for yval in range(y_low, y_high): if condition: coords.append((xval,yval))
您对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)