为了账号安全,请及时绑定邮箱和手机立即绑定

Python 循环认为一个网格 -200|200

Python 循环认为一个网格 -200|200

慕哥6287543 2022-01-05 12:09:18
我有坐标 -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))


查看完整回答
反对 回复 2022-01-05
?
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)


查看完整回答
反对 回复 2022-01-05
  • 2 回答
  • 0 关注
  • 178 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号