Python 超过json树中的最大递归深度
当我运行他的代码,即查找json树的子节点数时, 我犯了一个错误 调用Python对象时超出了最大递归深度。Python 超过json树中的最大递归深度,python,json,Python,Json,当我运行他的代码,即查找json树的子节点数时, 我犯了一个错误 调用Python对象时超出了最大递归深度。 请帮我做同样的事情。您的树有998层以上的深度: def get_children(node): for child in node['children']: yield child for grandchild in get_children(child): yield grandchild for line in
请帮我做同样的事情。您的树有998层以上的深度:
def get_children(node):
for child in node['children']:
yield child
for grandchild in get_children(child):
yield grandchild
for line in f:
d = json.loads(line)
child_dic={}
for child in get_children(d):
if child not in child_dic.keys():
child_dic[child["id"]]=1
在这种情况下不要使用递归。使用堆栈:
>>> def build_nested(depth):
... d = {'children': []}
... for i in range(depth - 1):
... d['children'].append({'children': []})
... d = d['children'][0]
... return d
...
>>> try:
... len(list(get_children(build_nested(998))))
... except RuntimeError:
... print 'too deep'
...
997
>>> try:
... len(list(get_children(build_nested(999))))
... except RuntimeError:
... print 'too deep'
...
too deep
与递归版本一样,这种简单的方法首先以相同的顺序遍历树深度
这仅受您可以为Python提供的内存量的限制:
def get_children(node):
stack = [node]
while stack:
node = stack.pop()
stack.extend(node['children'][::-1])
yield node
然而,这无助于加载这样的对象<代码>json库也有限制:
>>> try:
... len(list(get_children(build_nested(999))))
... except RuntimeError:
... print 'too deep'
...
998
>>> try:
... len(list(get_children(build_nested(10**6))))
... except RuntimeError:
... print 'too deep'
...
999999
导入json
>>>加载(“{”a:“*100000+'1'+'}”*100000)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/…/lib/python2.7/json/_init__.py”,第338行,加载
返回\u默认\u解码器。解码
文件“/…/lib/python2.7/json/decoder.py”,第366行,在decode中
obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
文件“/…/lib/python2.7/json/decoder.py”,第382行,原始解码
obj,end=self.scan_一次(s,idx)
RuntimeError:调用Python对象时超出了最大递归深度
您可以尝试使用提高Python递归限制,但要小心。将其设置得太高,您将使用segfault使Python解释器崩溃。从查看开始,并以此为起点增加限制,直到可以加载数据为止
我不知道还有任何其他Python JSON库可以处理像您这样深度的JSON对象
jsonlib2
just segfaults,ujson
有1024个对象的硬编码深度限制,demjson
也会给你一个最大递归深度
错误。你能给我们一个示例树来说明这个问题吗?你为什么要关闭这个你之前发布的问题,只是为了重新打开它?递归代码是正确的,但是你必须有一个非常深的嵌套树,这才是一个问题。这是一个非常大的树。因此不能发布。那么在这种情况下我该怎么办?我再次遇到同样的问题。树是]@Saurabh:我在回答中提供的函数不能有相同的问题,因为它不使用任何递归。ef get_children(node):stack=[node],而stack:node=stack.pop()stack.extend(node['children'][:-1])产生node f=open('badtree.json','r')size\u dic={}对于f中的行:d=json.load(line)child_dic={}对于get_children(d)中的child:if child不在child_dic.keys():child_dic[child[“id”]]=1@Saurabh:那么哪一行抛出异常json.loads()
maybe?d=json.loads(line)文件“/usr/lib/python2.7/json/\uuuu init\uuu.py”,第338行,在loads return\u default\u decoder.decode(s)文件“/usr/lib/python2.7/json/decoder.py”,第365行,在decode obj中,end=self.raw\u decode(s,idx=\w(s,0.end())文件“/usr/lib/python2.7/json/decoder.py”,第381行,在raw_decode obj中,end=self.scan_once(s,idx)运行时错误:调用Python对象时超出了最大递归深度
>>> import json
>>> json.loads('{"a":' * 100000 + '1' + '}' * 100000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/.../lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/.../lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
RuntimeError: maximum recursion depth exceeded while calling a Python object