为什么我的递归函数返回“无”? [英] Why does my recursive function return "None"?

查看:177
本文介绍了为什么我的递归函数返回“无”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,



我有一个Python的词典列表字典。这代表亲子关系。给我一个孩子,我想返回父母。



这是我的收藏:

  tree = {u'one':[{u'two':[{u'three':[]},{u'four':[]}]},{u'five': [{u'six':[]}]}}} 

如你所见,一有孩子二和五,二有孩子三,四,三没有孩子,等等。



以下代码正确地解决了给定子代的父代:

  def find_parent(search_term,collection,parent = None):
如果isinstance(collection,dict):
为key,value在collection.iteritems()中:
如果key.lower()== search_term.lower():
print %s的父级是%s%(key,parent)
return parent
if isinstance(value,list):
for v in value:
find_parent(search_term, v,key)

my_child =two
my_parent = find_parent(my_child,tree)

该函数中的print语句总是打印正确的值。但是如果我尝试访问my_parent,它的值总是无。这里必须超出范围。我只是无法解决如何解决它。



谢谢。

解决方案>

您还需要返回递归调用值:

  if isinstance(value,列表):
对于v中的值:
parent = find_parent(search_term,v,key)
如果父级不是None:return parent
/ pre>

没有返回,您忽略,丢弃递归搜索返回值。



演示与返回添加:

 >>> def find_parent(search_term,collection,parent = None):
... if isinstance(collection,dict):
...为key,collection.iteritems()中的值:
。 ..如果key.lower()== search_term.lower():
...打印%s的父代是%s%(key,parent)
... return parent
... if isinstance(value,list):
... for v in value:
... parent = find_parent(search_term,v,key)
...如果父级不是None:return parent
...
>>> my_child =two
>>>树= {u'one':[{u'two':[{u'three':[]},{u'four':[]}]},{u'five':[{u'six' :[]}]}]}
>>> find_parent(my_child,tree)
两个父项是一个
u'one'


All,

I have a dictionary of lists of dictionaries in Python. This represents a parent-child relationship. Given a child, I would like to return the parent.

Here is my collection:

tree = { u'one' : [ { u'two' : [ { u'three' : [] }, { u'four' : [] } ] }, { u'five' : [ { u'six' : [] } ] } ] }

As you can see, "one" has children "two" and "five", "two" has children "three" and "four", "three" has no children, and so on.

The following code correctly works out the parent of a given child:

def find_parent(search_term,collection,parent=None):
  if isinstance(collection,dict):
    for key,value in collection.iteritems():
      if key.lower() == search_term.lower():
        print "the parent of %s is %s" % (key,parent)
        return parent
      if isinstance(value,list):
        for v in value:
          find_parent(search_term,v,key)

my_child = "two"
my_parent = find_parent(my_child,tree)

The print statement in that function always prints the correct values. But if I try to access my_parent, its value is always "None". Something must be going out of scope here. I just can't work out how to fix it.

Thanks.

解决方案

You need to return the recursive call value too:

if isinstance(value,list):
    for v in value:
        parent = find_parent(search_term,v,key)
        if parent is not None: return parent

Without the return you ignore, discard, the recursive search return value.

Demo with the return added:

>>> def find_parent(search_term,collection,parent=None):
...   if isinstance(collection,dict):
...     for key,value in collection.iteritems():
...       if key.lower() == search_term.lower():
...         print "the parent of %s is %s" % (key,parent)
...         return parent
...       if isinstance(value,list):
...         for v in value:
...           parent = find_parent(search_term,v,key)
...           if parent is not None: return parent
... 
>>> my_child = "two"
>>> tree = { u'one' : [ { u'two' : [ { u'three' : [] }, { u'four' : [] } ] }, { u'five' : [ { u'six' : [] } ] } ] }
>>> find_parent(my_child,tree)
the parent of two is one
u'one'

这篇关于为什么我的递归函数返回“无”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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