-----> Conventions:
function, only one positional parameter is represented by argv
def func (argv):
Pass
1. Generator: Custom iterators
There are two forms of generators:
A. Generator functions
B. Generator expression
A.
def func1 (x): x + = 1 print (111111) yield x #有yield的叫生成器函数 print (222222) yield ' alex ' s = func1 (5) # Called the generator function object so that the function is not executed s = s.__next__ () #一个next对应一个yield #yield returns the value to S.__next__ ()
Compare the difference between send () and yield:
Send () can be used when yield to use, send () in parentheses need to have elements, up a yield to send data, change the return value of the previous yield,
So the first value cannot be sent with Send (), and the last yield will never get the value of Send ()
Def func1 (): print (111) count = yield 1 print (count) print (222) yield 2 print (333) Yield 3g = func1 () a = g.__next__ () print (a) print (G.send (' Alex ')) #----> send can be used as __next__ while the Send (' Alex ') The elements inside are sent to the previous yieldprint (g.__next__ ())
Compare the difference between yeild and return:
Difference: return: A return is encountered in a function, then an end function is returned to the function performer.
Yield: There is yield in the function, and the function is not executed when the function is called, only when the function name (). __next__ executes a yeild and returns the value to it
A next corresponds to a yeild, which can be called multiple times, and the generator function is essentially an iterator.
Memory level vs. ITER () and generator functions;
When ITER () transforms an iterative function into an iterator, all the data of the iterated function needs to be loaded into memory to be converted, which consumes memory and cannot handle big data
The generator function does not need to load the data, but rather a
Basic application of generators: can be segmented to create a seamless connection
Comparison
1.def func (argv): For I in range (1,ARGV): print (' Knight class%s period '%i) func (5) 2.def func (argv): For I in range (1,ARGV) : yield ' Knight class%s period '%ig = func (5) for J in Range (1,5): print (g.__next__ ()) 3.def func (argv): For I in range (1,argv ): yield ' Knight class%s '%ifor j in Range (1,5): Print (func (5). __next__ ())
Attention:
def func (argv): For I in range (1,ARGV): yield ' Knight class%s period '%ig = func (Ten) for J in Range (1,5): print (g.__next__ ()) for J in Range (1,6): print (g.__next__ ())
B. Generator expression
g = [I for I in range (100)] is a list-derived
g = (I-I in range (100)) is a generator expression
g = (I for I in range) print (g.__next__ ()
2. List derivation:
A line of code opportunities can represent any defined list
There are two types of modes:
1. Cycle mode: variable (processed variable) + for I in iterable
List1 = [' python%s '%i for I in range (1,21)]print (list1) [' Python 1 ', ' python Phase 2 ', ' Python 3 ', ' python 4 ']list1 = [I*i for I in range (1,11)]print (List1) [1, 4, 9, 16]
2. Filter Mode: variable (processed variable) + for I in Iterable + if condition
One cycle:
List1 = [' python%s period '%i for I in range (1,21) if I% 2 = = 0]print (list1)
Two cycles (not more than two cycles):
names = [[' Tom ', ' Billy ', ' Jefferson ', ' Andrew ', ' Wesley ', ' Steven ', ' Joe '], [' Alice ', ' Jill ', ' Ana ', ' Wendy ', ' jennif Er ', ' Sherry ', ' Eva ']]list1 = [j for I in Names for J in I if J.count (' e ') = = 2]print (list1) # Compare Print (list (j for I in Name s for j in I if J.count (' e ') = = 2))
Advantages and disadvantages of the list derivation:
Advantages: A single row to solve a list, simple and convenient
Cons: Not easy to troubleshoot, not all list problems, do not deliberately use
Python Learning day 12th, generator, list derivation