Python递归深度错误:RecursionError: maximum recursion depth exceeded in comparison

本文介绍Python中默认的递归深度限制及其引发的RecursionError异常。通过修改脚本,使用迭代替代递归,并调整sys模块的递归深度设置,成功解决了递归深度超出的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python默认的递归深度是很有限的(默认是1000),因此当递归深度超过999的样子,就会引发这样的一个异常。

 

用Python写了一个递归脚本,脚本如下
def fact(n):
    return fact_iter(n, 1)
 
 
def fact_iter(num, product):
    if num == 1:
        return product
    return fact_iter(num - 1, num*product)

报错:报错提示超过最大递归深度

RecursionError: maximum recursion depth exceeded in comparison

解决方案:

import sys
 
sys.setrecursionlimit(9000000) #这里设置大一些

错误解决!!!!

在unet模型中,我的imgs是三个波段的真彩色rgb遥感影像,masks是分了5个等级的lst栅格图像。出现报错,ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\4743.tif and data/masks\4743_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6452.tif and data/masks\6452_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\3449.tif and data/masks\3449_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6266.tif and data/masks\6266_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\5923.tif and data/masks\5923_mask.tif ERROR:root:Error processing files: data/imgs\4737.tif and data/masks\4737_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\3448.tif and data/masks\3448_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6265.tif and data/masks\6265_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\5918.tif and data/masks\5918_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6453.tif and data/masks\6453_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\3449.tif and data/masks\3449_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6266.tif and data/masks\6266_mask.tif ERROR:root:Error processing files: data/imgs\1382.tif and data/masks\1382_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6451.tif and data/masks\6451_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\1381.tif and data/masks\1381_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\3443.tif and data/masks\3443_mask.tif ERROR:root:maximum recursion depth exceeded in comparison
最新发布
07-01
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值