yield of understanding (2) to explore the way for the next cycle, and recursive

Code Purpose:

   Recursive, get all the elements in a List

  At the same time explain for, and characteristics of yield.

Code:

 

net = [[[1,2],3],4,[5,6]]
def flatten(nested = net ,level = 0):
    level +=1
    count = 0 
    try:
        for sublist in nested:
            count += 1
            print ('level {} sublist {} is {} '.format(level,count ,sublist) )
            for element in flatten(sublist,level):
                print('back to level:{}'.format(level))
                yield element 
    except TypeError:
        print('fined node:{}'.format(nested))
        yield nested

  

Explain the code:

  net

    The list is to be decomposed.

  flatten(nested  ,level ) :

    Recursive function calls for layer by layer into the nested list until you find the data elements

    When flatten parameters nested, is assigned to the data elements (instead of when the list), for sublist in nested triggers the error: "TypeError"

    At this point enter except TypeError, Yield nested suspend the function, return to the previous function to continue.

    Note: Yield does not mean that the whole program is suspended, but only where the Yield pause function, and returns the value.

    At this time is a function for element in flatten (sublist, level), then the node element = found, and by means of yield element, layer by layer back out.

    When the shell layer is retreated, the code pause. Users need to continue to enter the next (g), to find the next one to call.

    Note that after the next (g), the program is from the innermost recursive yield resumes execution.

  First execution next (g), and the second execution inlet next (g), the flow below (to save space, remove the print ()):

 

    This figure shows two things:

      1. yield upward return one value, and the return path of the same.

       2. next (g), is from the earliest time yield at the reply.

    It can be understood as, in turn call when multiple yield, yield scene is placed in a "stack." When using the next recovery yield from the scene saved the bottom of the stack of resumes.

    Question: Other yield stack, whether it is discarded, there is no answer.

 

experiment:

  In python before executing code:  

net = [[[1,2],3],4,[5,6]]
def flatten(nested = net ,level = 0):
    level +=1
    count = 0 
    try:
        for sublist in nested:
            count += 1
            print ('level {} sublist {} is {} '.format(level,count ,sublist) )
            for element in flatten(sublist,level):
                print('back to level:{}'.format(level))
                yield element 
    except TypeError:
        print('fined node:{}'.format(nested))
        yield nested

  Implementation process, and results are as follows:

  

>>> g= flatten()
>>> next(g)
level 1 sublist 1 is [[1, 2], 3] 
level 2 sublist 1 is [1, 2] 
level 3 sublist 1 is 1 
fined node:1
back to level:3
back to level:2
back to level:1
1
>>> next(g)
level 3 sublist 2 is 2 
fined node:2
back to level:3
back to level:2
back to level:1
2
>>> next(g)
level 2 sublist 2 is 3 
fined node:3
back to level:2
back to level:1
3
>>> next(g)
level 1 sublist 2 is 4 
fined node:4
back to level:1
4
>>> next(g)
level 1 sublist 3 is [5, 6] 
level 2 sublist 1 is 5 
fined node:5
back to level:2
back to level:1
5
>>> next(g)
level 2 sublist 2 is 6 
fined node:6
back to level:2
back to level:1
6
>>> next(g)
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    next(g)
StopIteration
>>> 

  Visible, hierarchical list as follows:

 

Guess you like

Origin www.cnblogs.com/stonenox/p/11207710.html