使用循环来调用递归函数

时间:2021-05-20 18:04:10

I am trying to create a recursive function that takes three parameters: name of the dictionary, name of the original (This will be the key in the dict), and name of the final (trying to determine if it is possible to reach the final from the original)

我正在尝试创建一个递归函数,它接受三个参数:字典的名称,原始的名称(这将是dict中的键)和final的名称(试图确定是否有可能到达最终从原来的)

My code functions well and enters the correct if statements and everything (tested using print statements) however, instead of the function returning True or False, it returns None every time.

我的代码运行良好并输入正确的if语句和所有内容(使用print语句测试)但是,它不是返回True或False的函数,而是每次都返回None。

I determined that this is because rather than calling my recursive function with "return" I only call the name of the function. However, if I include return in my code, the function only runs with the first value from the dictionary's key.

我确定这是因为我没有用“return”调用我的递归函数,而只调用函数的名称。但是,如果我在代码中包含return,则该函数仅使用字典键中的第一个值运行。

Any and all help on this would be appreciated.

任何和所有帮助将不胜感激。

def evolve(dictname, babyname, evolvedname):
    if babyname == evolvedname:
        return True
    elif babyname in dictname.keys():
        if dictname[babyname]:
            for i in dictname[babyname]:
                evolve(dictname,i,evolvedname)
        else:
            return False
    else:
        return False

1 个解决方案

#1


0  

Collect all recursive call's results, and return True if any of them is true.

收集所有递归调用的结果,如果其中任何一个为真,则返回True。

Something like:

def evolve(dictname, babyname, evolvedname):
    if babyname == evolvedname:
        return True
    elif babyname in dictname.keys():
        if dictname[babyname]:
            results = [] #To collect results
            for i in dictname[babyname]:
                results.append(evolve(dictname,i,evolvedname))
            #Check if any of them is True
            for res in results:
                if res==True: return True
            return False #No true among childs
        else:
            return False
    else:
        return False

But I think this code can be simplified to just:

但我认为这段代码可以简化为:

def evolve(dictname, babyname, evolvedname):
    if babyname == evolvedname:
        return True
    return any(evolve(dictname,i,evolvedname) for i in dictname.get(babyname,[]))

Lastly, although I don't know what you are trying to do, you might get an infinite loop, this is like doing dfs but without marking any node as already explored(black) or currently exploring(gray).

最后,虽然我不知道你要做什么,但你可能会得到一个无限循环,这就像做dfs但没有标记任何节点已经探索过(黑色)或正在探索(灰色)。

#1


0  

Collect all recursive call's results, and return True if any of them is true.

收集所有递归调用的结果,如果其中任何一个为真,则返回True。

Something like:

def evolve(dictname, babyname, evolvedname):
    if babyname == evolvedname:
        return True
    elif babyname in dictname.keys():
        if dictname[babyname]:
            results = [] #To collect results
            for i in dictname[babyname]:
                results.append(evolve(dictname,i,evolvedname))
            #Check if any of them is True
            for res in results:
                if res==True: return True
            return False #No true among childs
        else:
            return False
    else:
        return False

But I think this code can be simplified to just:

但我认为这段代码可以简化为:

def evolve(dictname, babyname, evolvedname):
    if babyname == evolvedname:
        return True
    return any(evolve(dictname,i,evolvedname) for i in dictname.get(babyname,[]))

Lastly, although I don't know what you are trying to do, you might get an infinite loop, this is like doing dfs but without marking any node as already explored(black) or currently exploring(gray).

最后,虽然我不知道你要做什么,但你可能会得到一个无限循环,这就像做dfs但没有标记任何节点已经探索过(黑色)或正在探索(灰色)。