python解决maximum recursion depth exceeded in comparison报错

本文讲解了Python中递归调用的深度限制及其原因,并提供了解决递归深度超出限制的简单方法。通过调整sys模块的setrecursionlimit方法,可以有效避免递归错误。

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

刷题的时候设置递归有时候会出现报错:RecursionError: maximum recursion depth exceeded in comparison
原因是在Python里的递归调用是有限制的,可以使用sys模块里的getrecursionlimit方法查看的到。

>>> import sys
>>> sys.getrecursionlimit()

打开终端运行Python,可以看到默认限制值为1000。
深入了解可以参考这篇博文:https://cyrusin.github.io/2015/12/08/python-20151208/

解决方法很简单,直接设置如下即可:

sys.setrecursionlimit(2000)
在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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值