Today's main content:
1. Iterators and Generators supplement,
2. Builder expressions and list expressions. and various derivation
3. Built-in functions
1) iterators and generators supplement
1. see if a pair of images is an iterator, and whether it is an iterator. You can also import these two methods to implement
Example:
from collections import iterable Span style= "COLOR: #008000" ># Import to see if you can iterate over the pair like From collections import Iterator # Span style= "COLOR: #008000" > Import to see if it is an iterator s = " 123 " f = Isinstance (s,iterable) print (f) F2 = Isinstance (s , Iterator) print (F2) run result is truefalse
2. The characteristic of an iterative pair of images is the internal __iter__ method, an iterator characterized by an internal __iter__ method and __next__ method.
3. The following example: Note how the generator is built.
def Fun (): for in range: yield igen= Fun () # The operation here is to generate a generator, note that this is not performed. Just a generator.
4. The generator has two generation methods, one of which is the yield in the function, which is generated by the generator function. One is generated by a generator expression.
5. There are two ways to iterate over an iterator like this.
A s.__iter__ () this is the generation of iterators
One is ITER (the pair), which is implemented by the ITER function.
The advantages of 6 iterators include the following three advantages:
One. Save Memory
Two. Inertia mechanism
Three. Unidirectional irreversible
7. Through dir to see if an iterative pair of images is iterative, and whether it is a generator,
The code examples are as follows:
s ="123"print("__iter__"in Dir (s))print("__next__"in dir (s))
2). Builder expressions and list expressions. and various derivation
Take a look at the list expression first, as shown in the following example:
LST = [" School uniform%s" for in range]print(LST)
The specific format is as follows:
[Variable (processed variable) for variable in iterable] loop mode
There are two modes of the list derivation, one is the cyclic pattern mentioned above, and the other is the conditional mode.
Classification:
[Variable (processed variable) for variable in iterable] loop mode
[Variable (processed variable) for variable in iterable if condition] Filter mode
Here is the sample code:
# 10 all numbers within the square lst = [i**2 for i in range (11 print (LST) # 30 the square of all numbers divisible by 3 Lst1 = [i*i for i in Range (1,31) if i% 3 = = 0] print (LST1) # 100. Lst2 = [i for i in range (+) if i 2 = = 1] print ( LST2)
The generator is just the [] switch to the () list expression, which becomes the generator expression.
As the above code is replaced by the generator, and it appears as follows:
#Square of all numbers within 10Gen = (i**2 forIinchRange (11))#Build a generatorPrint(Gen.__next__())#The square of all numbers within 30 that can be divisible by 3 #Gen1 = (i*i forIinchRange (1,31)ifI% 3 = = 0)#Build a generatorPrint(Gen1.__next__())#all the odd numbers within 100.Gen2 = (i forIinchRange (100)ifI% 2 = = 1)#Build a generatorPrint(Gen2.__next__())#v If you want to take it all out, you can use for forIinchGen2:Print(i)
Why is there a generator:
The builder itself is built with Python code, which satisfies the individual needs and needs of your future work.
Here is a sample question:
names = [[' Tom ', ' Billy ', ' Jefferson ', ' Andrew ', ' Wesley ', ' Steven ', ' Joe '],[' Alice ', ' Jill ', ' Ana ', ' Wendy ', ' jennif Er ', ' Sherry ', ' Eva ']
Get the name in the list above with two E, and put it in the new list.
Example code:
names = [['Tom','Billy','Jefferson','Andrew','Wesley','Steven','Joe'],['Alice','Jill','Ana','Wendy','Jennifer','Sherry','Eva']]lst= [Name forIinchNames forNameinchIifName.count ("e") ==2]Print(LST)
Various derivation formula:
Dictionary derivation formula:
Python Learning Chapter 13th