Python RuntimeError:超出最大递归深度 [英] Python RuntimeError: maximum recursion depth exceeded
本文介绍了Python RuntimeError:超出最大递归深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Python 新手.我写了一个关于返回排序重复元素数组A中x出现次数的函数:
I am new about Python. I wrote a function about returns the number of occurences of x in the sorted repeated elements array A:
def FindFirstIndex(A, low, high, x, n):
low = 0
high = len(A) - 1
if low <= high:
mid = low + (high - low) / 2
if (mid == 0 or x > A[mid - 1]) and A[mid] == x:
return mid
elif x > A[mid]:
return FindFirstIndex(A, (mid + 1), high, x, n)
else:
return FindFirstIndex(A, low, (mid - 1), x, n)
return -1
def FindLastIndex(A, low, high, x, n):
low = 0
high = len(A) - 1
if low <= high:
mid = low + (high - low) / 2
if (mid == n - 1 or x < A[mid + 1]) and A[mid] == x:
return mid
elif x < A[mid]:
return FindFirstIndex(A, low, (mid - 1), x, n)
else:
return FindFirstIndex(A, (mid + 1), high, x, n)
return -1
def COUNT(A, x):
i = FindFirstIndex(A, 0, len(A) - 1, x, len(A))
if i == -1:
return i
j = FindLastIndex(A, i, len(A) - 1, x, len(A))
length = j - i + 1
return length
错误是:RuntimeError:超出了最大递归深度.有谁知道如何解决它?
The error is: RuntimeError: maximum recursion depth exceeded. Anybody knows how to solve it?
推荐答案
确定可以使用:
import sys
sys.setrecursionlimit(3000)
我相信 Python 的默认值是 1000,所以应该这样做.我警告你,如果你尝试在一个非常大的列表上测试你的实现,python 可能会崩溃,所以我鼓励你迭代地重新实现你的代码
I believe Python's default is 1000 so this should do. I caution you though if you try and test your implementation on a very large list python might crash so I would encourage you to re-implement your code iteratively
这篇关于Python RuntimeError:超出最大递归深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文