我在RuntimeError: maximum recursion depth exceeded in cmp使用列表时遇到了错误.更确切地说,p0 in points中,points.index(p0)方法调用,以及在points.remove(p0)上方法调用points列表已经提出了具体的字典中的错误p0在我的特定索引points列表.该points列表包含错误时的4700个词典,从12000个对象列表中将一个词典减少一个,直到出现错误.该p0字典包含对列表中另一个字典的引用,该字典在返回时包含对该p0对象的引用.该p0字典以及字典它包含一个参考的两次出现points由上述三种方法调用引发错误之前列表.
这个错误来自哪里?
编辑:这是引发错误的代码.
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
Run Code Online (Sandbox Code Playgroud)
这是定义find,其中dist计算两点之间的距离.
def find(p1, points, tolerance = 0.01):
for p2 in points:
if dist(p2['coords'], p1['coords']) <= tolerance:
return p2
return False
Run Code Online (Sandbox Code Playgroud)
这是错误的完整回溯:
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
Run Code Online (Sandbox Code Playgroud)
可能你有一个循环结构,你的一个dicts通过一个'next's 链引用自己,如下所示:
>>> a = {}
>>> b = {}
>>> a['next'] = b
>>> b['next'] = a
>>> a == b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: maximum recursion depth exceeded in cmp
Run Code Online (Sandbox Code Playgroud)
如果您打印出dict,循环引用将显示为...:
>>> a
{'next': {'next': {...}}}
Run Code Online (Sandbox Code Playgroud)
也许这有助于找到字典中有问题的部分.