简体   繁体   English

遍历列表时出现RecursionError

[英]RecursionError while iterating over list

I have a list of geodesic points by the format: [lat, long, elevation, index, land/sea binary classifier] in a grid formation with regular spacing throughout the dataset. 我有以下格式的测地点列表: [lat, long, elevation, index, land/sea binary classifier]呈网格形式,整个数据集中有规则间距。 I'm trying to find all neighbouring points that are land (elv > 0) to the current point in the list. 我正在尝试查找列表中当前点到陆地的所有相邻点(elv> 0)。

I keep getting this error: RecursionError: maximum recursion depth exceeded while getting the repr of a list and although I understand the sort of thing that could be causing this, I have no idea how this applies to this situation as I'm not explicitly using recursion. 我不断收到此错误: RecursionError: maximum recursion depth exceeded while getting the repr of a list ,尽管我了解可能导致这种情况的原因,但我不知道这如何适用于这种情况,因为我没有明确使用递归。 How am I able to remedy this error? 我该如何纠正该错误? How can I understand the problem better as well? 我也如何更好地理解问题?

(topLat, bottomLat, westLong, eastLong are the lats and longs for the first and last point in the grid/map to identify points at the edge of the map) (topLat,bottomLat,westLong,eastLong是表格/地图中第一个和最后一个点的经纬度,以标识地图边缘​​的点)

def buildNeighbours(point, dataset):
    neighbours = []
    ix = int(point[3])

    if point[0] != topLat and point[0] != bottomLat and point[1] != westLong and point[1] != eastLong:
        nw = dataset[ix - (rowLength + 1)]
        n = dataset[ix - rowLength]
        ne = dataset[ix - (rowLength - 1)]
        e = dataset[ix + 1]
        se = dataset[ix + (rowLength + 1)]
        s = dataset[ix + rowLength]
        sw = dataset[ix + (rowLength - 1)]
        w = dataset[ix - 1]
        neighbours = [nw, n, ne, e, se, s, sw, w]
        point.append(neighbours)

    else:
        point = []

    return point


for point in dataList:
    point = buildNeighbours(point, dataList)

print(dataList[2000])

To stringify a point (really a list ), print must first get the string representation of every element. 要对一个点进行字符串化(实际上是一个list ), print必须首先获取每个元素的字符串表示形式。 The last element of each point is a list of neighboring points, each of which is a list that contains yet another list of neighboring points... one of which is the original point. 每个点的最后一个元素是一个list相邻点的,每一个都是一个list ,其中包含另一个list相邻点......其中一个是原来的点。 And so it continues... 所以它继续...

list 's __repr__ attempts to limit recursive cases, eventually giving up and returning '...' . list__repr__尝试限制递归情况,最终放弃并返回'...' Assuming it uses the same defaults as reprlib.Repr objects , the maxlevel (max recursion depth) is 6. With 8 neighbors each, that could mean thousands of visits to a relatively small number of unique points. 假设它使用与reprlib.Repr对象相同的默认值,则maxlevel (最大递归深度)为6。每个对象有8个邻居,这意味着数千次访问相对较少的唯一点。

I was able to print a 3×3 grid, where the fan-out is limited because most of the points only have 3 or 5 neighbors (corners and sides). 我能够打印一个3×3的网格,在该网格中,扇出是有限的,因为大多数点只有3或5个邻居(角和边)。 My simplified point lists, which didn't contain altitude or land/sea elements, required about 700kiB to represent the whole grid... about 40KiB for the upper-left corner alone. 我的简化point列表不包含海拔或陆地/海洋元素,需要大约700kiB才能表示整个网格……仅左上角大约需要40KiB。 On a 4×4 grid, a single point ballooned up to about 16MiB. 在4×4网格上,单个点膨胀到大约16MiB。

That said, I'm guessing what your inputs look like, and I probably haven't reproduced what you're really doing. 就是说,我正在猜测您输入的内容,而我可能没有复制您实际所做的事情。 More importantly, I did not get a RecursionError like you did, perhaps because I gave up waiting for it. 更重要的是,我没有得到一个RecursionError像你这样,也许是因为我放弃了等待它。

With those caveats in mind, I suggest: 考虑到这些警告,我建议:

  • In each point's neighbors list, store the indices of the neighbors. 在每个点的neighbors列表中,存储neighbors索引 Look them up later whenever you need them. 以后随时根据需要查找它们。 (This is the simplest solution I could come up with.) Write a couple helper functions that calculate the northwest or south or whatever neighbor of a given index, since you'll be doing that a lot. (这是我能想到的最简单的解决方案。)编写一些辅助函数,计算西北或南部或给定索引的任何邻域,因为您会做很多事情。

  • Alternatively, consider creating a neighbors dictionary, mapping each point's index to a list of indices. 或者,考虑创建neighbors字典,将每个点的索引映射到索引列表。 You'd have to keep that dictionary alongside dataList at all times, but it would let you remove the list of neighbors from all of your points. 您必须dataList将字典保留在dataList旁边,但这将使您从所有点中删除邻居列表。

  • If you really have to store the neighbors themselves, create a Point class with custom __str__ and __repr__ methods that don't try to print the neighbors. 如果您真的必须自己存储邻居,则使用不尝试打印邻居的自定义__str____repr__方法创建Point类。 As a bonus, a class would let you refer to fields with names like lat , lng , and index instead of mysterious subscripts like [1] and [3] . 另外,一个类可以让您引用名称如latlngindex字段,而不用引用[1][3]类的神秘下标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM