在使用列表时,我遇到了错误
这里是
RuntimeError: maximum recursion depth exceeded in cmp
。更准确地说,针对特定字典p0
在我的points
列表的特定索引处,p0 in points
、points.index(p0)
方法调用以及points.remove(p0)
方法调用都会引发此错误。在错误发生时,points
列表包含4700个字典,从一个包含12000个对象的列表中逐个减少一个字典,直到出现错误。p0
字典包含对列表中另一个字典的引用,而该字典反过来又包含对p0
对象的引用。在任何三个方法调用之前,p0
字典以及它所包含的引用出现两次。
这个错误是从哪里来的?
编辑:以下是引发错误的代码:
for roadType in roadTypes:
points = roadPoints[roadType][:]
while len(roadTails[roadType]) > 0:
p0 = roadTails[roadType].pop()
p1 = p0['next']
points.remove(p0) # Where the error occurs
points.remove(p1)
while True:
p2 = find(p1, points, 0.01)
if p2:
points.remove(p2)
p3 = p2['next']
points.remove(p3)
if p3 in roadTails[roadType]:
roadTails[roadType].remove(p3)
break
else:
p0, p1 = p2, p3
continue
else: break
这里是
find
的定义,其中dist
计算两点之间的距离。def find(p1, points, tolerance = 0.01):
for p2 in points:
if dist(p2['coords'], p1['coords']) <= tolerance:
return p2
return False
以下是错误的完整回溯信息:
Traceback (most recent call last):
File "app.py", line 314, in <module>
points.remove(p0) # Where the error occurs
RuntimeError: maximum recursion depth exceeded in cmp
find
来自哪里? - Ericsys.setrecursionlimit(20)
并发布完整的回溯吗? - Eric