With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. Also, creating a list of 1 million elements takes up a lot of storage space, and if we just need to access the first few elements, the vast majority of the space behind it is wasted.
So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the generator: Generator.
There are a number of ways to create a generator. The first method is simple, as long as a list of the generated formula is []
changed ()
to create a generator:
List-generation:
1 for inch # 2print(s)
Change to Generator:
1 for inch # This is the list generation in Python, this side loop is computed as a mechanism, called the generator: Generator. 2print(s)3
Results:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62 , +, 94, 98]<generator Object <genexpr> A---------------- T 0x0000000001e0ee08>
We can print out every element of the list directly, but how do we print out every element of generator?
If you want to print out one, you can next()
get the next return value for generator by using a function:
1 for inch # This is the list generation in Python, this side loop is computed as a mechanism, called the generator: Generator. 2print(s)34print(S.__next__()) # __next__ is a private method of a function. 5print(next (s))6print(next (s))
<generator object <genexpr> at 0x000000000069eeb8>024
We have said that generator saves the algorithm, each time it is called next(g)
, computes g
the value of the next element until the last element is computed, and when there are no more elements, the error is thrown StopIteration
.
Of course, this constant invocation is next(s)
so perverted that the correct approach is to use for
loops, because generator is also an iterative object:
1S= (x*2 forXinchRange (10))#This is the list generation in Python, this side loop is computed as a mechanism, called the generator: Generator. 2 Print(s)3 4 #print (s.__next__ ()) #__next__ is a private method of the function. 5 #print (Next (s))6 #print (Next (s))7 forIinchS:8 Print(i)
Results:
<generator object <genexpr> at 0x000000000067ee08>024681012141618
So, after we create a generator, we basically never call next()
it, but iterate over it through the for
loop and don't need to be concerned about StopIteration
the error.
Generator is very powerful. If the calculated algorithm is more complex, and the loop with similar list generation for
cannot be implemented, it can also be implemented with functions.
For example, the famous Fibonacci sequence (Fibonacci), except for the first and second numbers, can be summed up by the top two numbers:
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The Fibonacci sequence is not written in a list, but it is easy to print it out with a function:
#13th day recursive functiondeff (N): Before=0 after=1ifN==1: return0ifn==2: return1 forIinchRange (0,n-2): Before,after= after,after+beforereturn After forIinchRange (1,9): Print(f (i))
Recursive functions:
def f (n): if n==1: return 0 if n==2: return 1 sum=f (n-1) +f (n-2) return sum for I in range (1,9): C6/>print (f (i))
Generate a Fibonacci sequence:
1 def fib (max): 2 n,before,after=0,0,13while n<Max:4 print(after )5 before,after=after,before+after6 n+=17 return ' Done ' 8 9 Print (FIB (6))
Printing results:
112358 Done
Looking closely, it can be seen that the fib
function is actually a calculation rule that defines the Fibonacci sequence, which can be derived from the first element, and the subsequent arbitrary elements, which are actually very similar to generator.
In other words, the above functions and generator are only a step away. To turn fib
a function into a generator, just print(after)
change yield after
it to:
1 deffib (max):2n,before,after=0,0,13 whilen<Max:4 #print (after)5 yield After6Before,after=after,before+ After7N+=18 return ' Done'9 Ten Print(FIB (6))
Result: <generator object fib at 0x000000000214ee08>
This is another way to define generator. If a function definition contains a yield
keyword, then the function is no longer a normal function, but a generator:
>>> f = fib (6)>>> F<generator object fib at 0x104feaaa0>
The most difficult thing to understand here is that the generator is not the same as the execution flow of the function. The function is executed sequentially, the statement is encountered return
or the last line of the function is returned. The function that becomes generator, executes at each invocation next()
, encounters a yield
statement return, and executes again from the last statement returned yield
.
We continue to call during the loop yield
, and we are constantly interrupted. Of course, you have to set a condition for the loop to exit the loop, or it will produce an infinite sequence.
Similarly, after changing a function to generator, we basically never use it next()
to get the next return value, but instead use the for
loop directly to iterate:
1 for in fib (6):2 ... Print (n) 3 ... 4 15 16 27 38 59 8
However for
, when calling generator with a loop, the return value of the statement that is not generator is found return
. If you want to get the return value, you must catch the StopIteration
error, and the return value is included in StopIteration
the value
:
>>> g = fib (6)>>> whileTrue: ...Try: ... x=Next (g) ...Print('g:', x) ...exceptstopiteration as E: ...Print('Generator return value:', E.value) ... Break... g:1g:1g:2g:3g:5g:8GeneratorreturnValue:done
Yang Hui triangle:
1 " "2 The Yang Hui triangle is defined as follows:3 4 15 / 6 1 17 / \ / 8 1 2 19 / \ / \ / Ten 1 3 3 1 One / \ / \ / \ / A 1 4 6) 4 1 - / \ / \ / \ / \ / - 1 5 5 1 the think of each line as a list, try writing a generator, and continuously output the list of the next line: - - " " - + deftriangles (): -l=[0,1, 0] +N=1 A whileN<10: at -L_print=[] - #yield ("Print section%s, data is%s"% (len (L) -2,l[1:-1])) - - Print("Print section%s, data is%s"% (Len (L) -2,l[1:-1])) - forIinchRange (len (L)-1): inL_print.append (l[i]+l[i+1]) - #L=[0]+l_print+[0] to L_print.insert (0,0) + l_print.append (0) -L=L_print theN+=1 * $ Panax Notoginsengp=triangles () - #print (Next (p)) the #print (Next (p)) + #print (Next (p)) A #print (Next (p)) the #print (Next (p)) + #print (Next (p)) - Print(p)
Results:
Print 1th, data is [12, data is [1, 13, data is [1, 2, 14, data is [1, 3, 3, 15, data is [1, 4, 6, 4, 16, data is [1 , 5, ten, 5, 17, data is [1, 6,,, 6, 1, 8, data is [1, 7, 7, 1, 9, data is [1, 8, 28, 56 , 8, 1]none
Python Getting Started 17th day _ Builder