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

Jupyter中计算光度距离时TypeError错误的解决方法

问题解决方法:TypeError: unsupported operand type(s) for +: 'int' and 'list'
  • 直接传入redshifts=redshifts_1,不要额外添加方括号
    原因:redshifts_1本身就是包含100个数值的一维列表,传入时加[]会将其包装成二维列表(结构类似[[z1, z2, ..., z100]])。函数内部遍历该参数时,每次拿到的是整个子列表而非单个红移数值,导致后续与整数进行加法运算时触发类型错误。

  • 若函数需要支持批量处理多个红移列表(即传入二维列表),需修改函数内部逻辑,先遍历外层列表,再逐个处理内层的红移值:

    def generate(redshifts):
        lum_distances = []
        lum_errors = []
        # 遍历外层的每个红移列表
        for redshift_group in redshifts:
            # 遍历单个列表内的每个红移值
            for z in redshift_group:
                # 替换为你原有的光度距离计算逻辑
                luminosity_dist = calculate_luminosity(z)
                # 替换为你原有的误差计算逻辑
                error = calculate_error(z)
                lum_distances.append(luminosity_dist)
                lum_errors.append(error)
        return lum_distances, lum_errors
    

    但如果仅需处理单个红移列表,优先使用第一种方法,无需修改函数代码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 18:49:12