You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

for循环双索引遍历2D坐标数组报错无法解包非可迭代int对象如何解决

报错根因

range(330)每次仅输出单个整数,无法直接拆包为i,j两个变量,这是触发报错的直接原因。
其次你对xy数组的索引逻辑也存在问题:xy形状为(330,2),xy[i]才是第i组完整的(x,y)坐标对,xy[i,j]取到的是第i行第j列的单个数值,不符合CircularAperture的参数要求。

解决方案

方案1:直接遍历坐标对(无需索引,最简洁)

直接迭代xy数组的每一行,每一行就是一组完整的坐标:

for coord in xy:
    aperture = phot.aperture.CircularAperture(coord, 3)
    photometry = phot.aperture_photometry(xy, aperture)

如果需要单独拿到x、y值做其他处理,可以直接在循环里拆包:

for x, y in xy:
    # 这里可以单独用x、y做其他操作
    aperture = phot.aperture.CircularAperture((x, y), 3)
    photometry = phot.aperture_photometry(xy, aperture)

方案2:按索引遍历(需要用到坐标序号时使用)

如果你的逻辑需要用到当前坐标的序号i,只遍历单个索引变量即可:

for i in range(330):
    coord = xy[i]
    aperture = phot.aperture.CircularAperture(coord, 3)
    photometry = phot.aperture_photometry(xy, aperture)

方案3:批量处理(无需循环,性能最优)

photutils的CircularAperture本身支持传入多组坐标批量生成孔径,不需要写循环逐次处理,运行效率更高:

# 直接传入整个xy数组,一次性生成所有坐标对应的孔径
aperture = phot.aperture.CircularAperture(xy, 3)
# 一次性完成所有坐标的测光
photometry = phot.aperture_photometry(xy, aperture)

内容的提问来源于stack exchange,提问作者Jimbles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 16:24:01