使用列表时出现"RuntimeError:超出最大递归深度,以cmp为单位" [英] 'RuntimeError: maximum recursion depth exceeded in cmp' when working with lists

查看:192
本文介绍了使用列表时出现"RuntimeError:超出最大递归深度,以cmp为单位"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用列表时遇到错误RuntimeError: maximum recursion depth exceeded in cmp.更准确地说,p0 in pointspoints.index(p0)方法调用以及points列表上的points.remove(p0)方法调用一直在我的points列表的特定索引处引发特定字典p0的错误. . points列表在发生错误时包含4700个字典,从12000个对象的列表中将一个字典减少了一个,直到出现错误为止. p0词典包含对列表中另一个词典的引用,而返回列表中包含对p0对象的引用. p0字典及其包含的引用的字典将在三个方法调用中的任何一个引发错误之前,在points列表中出现两次.

I've encountered the error RuntimeError: maximum recursion depth exceeded in cmp when working with lists. More precisely, p0 in points, the points.index(p0) method call as well as the points.remove(p0) method call on the points list have been raising the error for a specific dictionary p0 at a specific index of my points list. The pointslist contains 4700 dictionaries at the time of the error, reduced one dictionary by one from a list of 12000 objects until the error is raised. The p0 dictionary contains a reference to another dictionary in the list, which in returns contains a reference to the p0 object. The p0 dictionary as well as the dictionary it contains a reference to appear twice in the points list before the error is raised by any of the three method call.

此错误来自何处?

编辑:这是引发错误的代码.

EDIT: Here's the code that raises the error.

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计算两个点之间的距离.

Here's the definition of find, where dist calculates the distance between two points.

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

推荐答案

可能您有一个循环结构,其中一个字典通过'next'链来引用自身,如下所示:

Probably you have a circular structure where one of your dicts refers to itself through a chain of 'next's, like this:

>>> 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

如果打印出该字典,则循环引用将显示为...:

If you print out the dict, a circular reference would show up as ...:

>>> a
{'next': {'next': {...}}}

也许这可以帮助您找到字典中有问题的部分.

Maybe this can help to find the problematic part of the dict.

这篇关于使用列表时出现"RuntimeError:超出最大递归深度,以cmp为单位"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆