Decipher the iterators of the working mechanism of for loops, as well as generators, ternary expressions and list comprehension, decompression sequences

Contents of this section


1. Iterator protocol and for loop

2. Ternary expressions

3. Decompression sequence

4. List Comprehension

5. Generator

 

Iterator protocol and for loop

1. The iterator protocol means: the object must provide a next method, and executing this method either returns the next item in the iteration, or causes an exception to report an error (because the iteration is over, you can only go backwards and not go backwards)

2. Iterable objects: objects that implement the iterator protocol (in fact, all objects in python are not iterable objects, how to implement: define an __iter__ method inside the object )

3. A protocol is a contract, an iterable object implements the iterator protocol, and python internal tools (such as for loop, sum, max, etc.) use the iterator protocol to access objects.

i = [11,22,33,44]
a = i.__iter__() # Turn i into an iterable object
print(a.__next__())             #11
print(a.__next__())             #22    
print(a.__next__())             #33  
print(a.__next__())             #44

 

So how does a for loop do it?


The for loop does two things. The first thing is to use the __iter__ method on the incoming for loop object to make it an iterable object. After that, the __next__ method is referenced every time it is used.

The second thing is: at the end of next, that is, an error will be reported at the end of the iteration, and the for loop will automatically handle the error for us

for a in i:
    print(a)

 

Note: The working mechanism of the while loop is not an iterative loop, but an index loop, so it cannot loop over iterative objects such as dictionaries, sets, files, etc. Using a for loop can also save memory because traversing one throws another

 

 

Ternary expression

l = ['鸡蛋%s' %i for i in range(10) if i>5 ]
print(l)

 


 

 

Ternary expressions are actually shorthand programming

name = 'ecohboy'

a = '帅哥'if name == 'ecohboy' else 'sb'
print(a)

The left side of if is a true operation, the middle is a judgment statement, and the right side is an otherwise operation, but it is only suitable for simple logic.

 

list comprehension


 

l = ['鸡蛋%s' %i for i in range(10) if i>5 ]
print(l)

 

decompression sequence


l = [1,2,3,4,5,6,7]
a,*b,c = l
print(a,b,c)

The decompression sequence is a one-to-one correspondence of values, but in this way, the values ​​at the beginning and end can be obtained without indexing

 

Builder


 

The generator can be understood as a data type, but it automatically implements the iterator protocol (without calling the __iter__ method), so the generator is an iterable object

1. The generator function is used with yield (return value):

 

def test():
    yield 1
    yield 2
    yield 3
    yield 4

g = test()
print(g)
print(g.__next__()) #1
print(g.__next__()) #2
print(g.__next__()) #3
print(g.__next__()) #4

 

2. Generator Expressions

laomuji = ( ' egg %s ' %i for i in range(10 ))
 print (laomuji. __next__ ())   #egg 0 
print (laomuji. __next__ ())   #egg 1 
print (laomuji. __next__ ())
 print ( laomuji.__ next__ ())

The generator expression is actually changing the list comprehension [] to ()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325300796&siteId=291194637