Recursion using yield

前端 未结 4 1025
粉色の甜心
粉色の甜心 2020-11-28 05:41

Is there any way to mix recursion and the yield statement? For instance, a infinite number generator (using recursion) would be something like:

         


        
相关标签:
4条回答
  • 2020-11-28 05:51

    So basically you just need to add a for loop at where you need to call your function recursively. This applies for Python 2.7.

    0 讨论(0)
  • 2020-11-28 06:07
    def lprint(a):
        if isinstance(a, list):
            for i in a:
                yield from lprint(i)
        else:
            yield a
    
    b = [[1, [2, 3], 4], [5, 6, [7, 8, [9]]]]
    for i in lprint(b):
        print(i)
    
    0 讨论(0)
  • 2020-11-28 06:09

    In some cases it might be preferable to use a stack instead of recursion for generators. It should be possible to rewrite a recursive method using a stack and a while loop.

    Here's an example of a recursive method which uses a callback and can be rewritten using stack logic:

    def traverse_tree(callback):
        # Get the root node from somewhere.
        root = get_root_node()
        def recurse(node):
            callback(node)
            for child in node.get('children', []):
                recurse(child)
        recurse(root)
    

    The above method traverses a node tree where each node has a children array which may contain child nodes. As each node is encountered, the callback is issued and the current node is passed to it.

    The method could be used this way, printing out some property on each node.

    def callback(node):
        print(node['id'])
    traverse_tree(callback)
    

    Use a stack instead and write the traversal method as a generator

    # A stack-based alternative to the traverse_tree method above.
    def iternodes():
        stack = [get_root_node()]
        while stack:
            node = stack.pop()
            yield node
            for child in reversed(node.get('children', [])):
                stack.append(child)
    

    (Note that if you want the same traversal order as originally, you need to reverse the order of children because the first child appended to the stack will be the last one popped.)

    Now you can get the same behavior as traverse_tree above, but with a generator:

    for node in iternodes():
        print(node['id'])
    

    This isn't a one-size-fits-all solution but for some generators you might get a nice result substituting stack processing for recursion.

    0 讨论(0)
  • 2020-11-28 06:12

    Yes, you can do this:

    def infinity(start):
        yield start
        for x in infinity(start + 1):
            yield x
    

    This will error out once the maximum recursion depth is reached, though.

    Starting from Python 3.3, you'll be able to use

    def infinity(start):
        yield start
        yield from infinity(start + 1)
    

    If you just call your generator function recursively without looping over it or yield from-ing it, all you do is build a new generator, without actually running the function body or yielding anything.

    See PEP 380 for further details.

    0 讨论(0)
提交回复
热议问题