python_ list generator / iterator

List of Formula

For example there is a list

a =[2,3,4,5]

We need to add a list of all 1

# A first method 
for I in Map ( the lambda I: I +. 1, A)
# The second method 
for index, I in the enumerate (A): 
    A [index] + =. 1 Print (A)

# Third, a simple list formula 
A = [I +. 1 for I in A]
 Print (A) 
# add a ternary operator
a = [i * 2 if i > 3 else i for i in a]

 1. _ inert list generator operation

Builder 1.1

In parentheses is the list generation type, is a small parenthesis List Builder

a = ( i *2  for i in range(a))
DEF FIB (NUM): 
    COUNT = 0 
    A, B = 0,. 1 # A = 0, B =. 1 
    the while COUNT < NUM: 
        tmp = A 
        A = B 
        B = A + tmp
         # Print (A) 
        COUNT + =. 1 the yield a # return a, while suspending the current function, a return to the by __next __ () calls the current function of people # return a Print ( " DONE ... " ) 
f = fib (10 )
 Print (f. __next__ ( ))
        
        
    

Iterator

 All generators are iterators, iterators are not necessarily generator (different angle, by the method iter, Python

Memory Object think iterator, essentially on the same thing)

For example python 3 inside the range (0,10) This is in fact an iterator

 

 

 

 



Guess you like

Origin www.cnblogs.com/cdm023/p/12019783.html