简体   繁体   中英

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. 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. However, I don't know how to (if it is possible) return a value without stopping the recursion.

You're looking for 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 .

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. 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. (Look at phihags proposal for an example)

  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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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