6th lesson
1, Loop object: Including a next method , the purpose of this method is to proceed to the next result, after the end throws Stopinteration exception;
When the loop structure calls a loop object as a for loop, the next method is called every time the loop is thrown, knowing that the last throw stopinteration exception, the loop structure knows the end, and stops calling the next method.
Such as:
1 # text.txt content is 2 1233 1234 1235 for in open ('text.txt ' ): 6 Print Line
The Open method returns a Loop object that contains the next method, and the For loop automatically calls the next method until the stopinteration exception occurs.
The advantage of using a looping object is that you don't have to create the element you want to use when the loop doesn't start. The elements used can be generated successively during the loop. This saves space, improves efficiency, and makes programming more flexible.
2. Iterators:
Technically, there is also a middle layer between the loop object and the For loop call, which is to convert the looping object to an iterator (iterator). This conversion is accomplished by using the ITER () function. However, this layer can often be ignored on a logical level, so loop objects and iterators often refer to each other.
3. Generator:
Constructs a user-defined loop object
1) The generator is defined in the same way as the function definition, except that it is replaced with yield in return;
2) There can be multiple yield in the generator, when When the generator encounters a yield, it pauses the generator and returns the value after yield when the generator is called again, it continues from where it was just until the next yield is encountered;
3) The generator itself is a circulator , and each loop returns a value after the yield
here is a generator: def Gen (): = Yield a = a*8 yield a yield # The generator has 3 yield, and when used as a circulator, it makes 3 round loops for in gen (): Print I
1 def Gen (): 2 for in range (4):3 yield i
This code can also be written as a generator expression (Generator expression): G = (x for x in range (4)); Builder expression is a simple way to write a generator
4. Table derivation (list comprehension): is a quick way to generate a table (list).
If you want to generate a table:
L = [] for in range (4): l.append (i**2)
There is also an easier way to do this: the method of table derivation
1 for in range (4)]
Exercises:
1 x1 = [1,3,5]2 y1 = [9,12,13]3for inif(y) >10]
L = [9,25]
View Code
7th Lesson: Function Objects
Functions are also objects, and functions have properties and methods that can be assigned not only to other object names but also to parameters.
1. Lambda function: Define 1 functions
1 Lambda x,y:x+y2print func (3,4)34 equivalent to 5 def func (x, y):6 return x+y7 Print func (3,4)
Lambda generates 1 function objects, the parameter is x, Y, the return value is X+y, and the value of the function object is assigned to the Func
2. function as parameter Pass
A function can be passed as an object, and a function name is an object:
def test (F,A,B):
print ' Test '
Print F (A, B)
Func = Lambda x,y:x+y
Test (func,1,2)
The first parameter of the test function is a function object that assigns Func to F, or it can be written like this: Test ((Lambda x,y:x+y), min)
3,map function : Format: map( func, seq1[, seq2...] ) , is the python built-in function, the first parameter is a function object, the second, the third ... is a series.
The function of the map function is to apply the functions object to each element of the subsequent sequence, storing the result of each action in the sequence ( in Python 3.X, the return value of map () is a loop object. You can use the list () function to convert the loop object into a table. )
As follows:
Re = map ((Lambda x:x+3), [x-y])print redef func: return x**2,y**2= map (func,[1,2,3,4],[2,3,4,5])print re output: [4,5,6 [(1,4), (4,9), (9,16), (16,25)]
4, thefilter function : The first parameter is also a function object, the function object to the subsequent sequence of each element, if the function object returns True, then the element is stored in the returned sequence, the filter function through the function object to filter the data (Similarly, in python3.x, the filter returns either a list or a loop object), as shown in the following example:
1 def func (x): 2 if x>100:3 return True4 Else : 5 return False 6 ru = filter (func,[100,104,99,101,123])7 output:8 [104, 101, 123]
The Ps:filter function has a maximum of 2 parameters, 1 function objects, and 1 series, unlike the map function, which can have multiple sequence parameters.
5, Reduce function: format: reduce (func, seq[, Init]) , The first argument is also a function object and requires it to accept 2 parameters , and then each iteration, the last iteration result (the element that was init the first time, such as the first element of the SEQ without init) executes a two-dollar func function with the next element. In the reduce function, init is optional and, if used, is used as the first element of the first iteration. Returns the result of multiple iterations , as shown in the following example:
1 m = 2 2 n = 5 3 print reduce (lambda#4print reduce (lambda # 240 Equivalent ((((2*1) **)*5,2 is the init element
PS: Thereduce () function is not directly used in 3.0, it is defined in the Functools package and needs to be introduced into the package.
Python Basic Learning (6th day)