更新时间:2023-02-19 11:34:46
您必须检查递归调用是否确实找到了某些东西,以便您可以继续循环。例如。尝试以下操作:
You have to check whether the recursive call actually found something so you can continue the loop. E.g. try the following:
def tree_traverse(tree, key):
for k, v in tree.items():
if k == key:
return v
elif isinstance(v, dict):
found = tree_traverse(v, key)
if found is not None: # check if recursive call found it
return found