简体   繁体   English

如何在不停止递归的情况下返回递归函数中的值?

[英]How can I return values in a recursive function without stopping the recursion?

I have a structure with an x amount of lists in lists, and each list an x amount of tuples. 我有一个列表中有x个列表的结构,每个列表都有x个元组。 I don't know beforehand how many nested lists there are, or how many tuples in each list. 我事先不知道有多少嵌套列表,或者每个列表中有多少元组。

I want to make dictionaries out of all the tuples and because I don't know the depth of the lists I want to use recursion. 我想在所有元组中使用字典,因为我不知道列表的深度我想使用递归。 What I did was 我做的是

def tupleToDict(listOfList, dictList):
    itemDict = getItems(list)  # a function that makes a dictionary out of all the tuples in list
    dictList.append(itemDict)
    for nestedList in listOfList:
         getAllNestedItems(nestedList, dictList)

    return dictList

this works, but I end up with a huge list at the end. 这是有效的,但我最终得到了一个巨大的列表。 I would rather return the itemDict at every round of recursion. 我宁愿在每轮递归时返回itemDict。 However, I don't know how to (if it is possible) return a value without stopping the recursion. 但是,我不知道如何(如果可能)返回值而不停止递归。

You're looking for yield : 你在寻找yield

def tupleToDict(listOfList):
    yield getItems(listofList)
    for nestedList in listOfList:
        for el in getAllNestedItems(nestedList):
            yield el

In Python 3.3+, you can replace the last two lines with a yield from . 在Python 3.3+中,您可以使用yield from替换最后两行。

You may want to rewrite your function to be iterative: 您可能希望重写您的函数以进行迭代:

def tupleToDict(listOfList):
    q = [listOfList]
    while q:
        l = q.pop()
        yield getItems(l)
        for nestedList in listOfList:
            q += getAllNestedItems(nestedList)

Who are you going to return it to? 你打算把它归还给谁? I mean if your thread is busy running the recursive algorithm, who gets the "interim results" to process? 我的意思是如果你的线程忙于运行递归算法,谁得到“临时结果”来处理?

Best bet is to tweak your algorithm to include some processing before it recurses again. 最好的办法是在再次递归之前调整算法以包含一些处理。

I'm not sure what you're trying to do, but you could try to make a recursive generator by using the yield statement to return the dict at the desired intervals. 我不确定你要做什么,但你可以尝试通过使用yield语句以所需的时间间隔返回dict来制作递归生成器。 Either that or shove copies of it into a global list? 要么将其复制到全球列表?

You got two possible solutions: 你有两个可能的解决方案:

  1. The generator approach: a function with a yield statement, which may be a hassle to implement in a recursive function. 生成器方法:具有yield语句的函数,这可能是在递归函数中实现的麻烦。 (Look at phihags proposal for an example) (以phihags提案为例)

  2. The callback approach: You call a helper-function/method from inside the recursion and can monitor the progress through a second outer function. 回调方法:从递归内部调用辅助函数/方法,并可以通过第二个外部函数监视进度。

Here a non-recursive recursion example: ;-) 这里是一个非递归递归示例:;-)

def callback(data): print "from the depths of recursion: {0}".format(data)

 def recursion(arg, callbackfunc): arg += 1 callbackfunc(arg) if arg <10: recursion(arg, callbackfunc) return arg print recursion(1, callback) 

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

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