python [iterator generator list comprehension]

python [iterator generator list comprehension]

1. Iterator

    1. The index index of how the iterator 
takes the value from the list and dictionary, and the key for loop . Anything that can use the for loop to take the value is an iterable iterable protocol: the internal __iter__ method is an iterable iterator Protocol: Iterators contain __iter__ methods and __next__ methods internally




print (dir ([1,2,3]))
lst_iter = [1,2,3].__iter__()
print(lst_iter.__next__())
print(lst_iter.__next__())
print(lst_iter.__next__())
for i in [1,2,3]:   #  [1,2,3].__iter__()
    print(i)
l = [1,2,3]
lst_iter = journey(l) # l.__iter__()
while True:
    try:
        print(next(lst_iter)) # lst_iter.__next__()
    except StopIteration:
        break

  

2. What is an iterable 
  What is an iterator ===》Iterator = iter (iterable), comes with a __next__ method
 The biggest advantage of iterable saves memory
from collections import Iterable,Iterator
print(range(100000000))
print(isinstance(range(100000000),Iterable)) #Determine whether the current is an iterable
print(isinstance(range(100000000), Iterator)) #Is it an iterator
py2 range #No matter how many ranges will generate a list, this list will be used to store all values
py3 range #No matter how much the range is, it will not actually generate any value

 3. Iterator advantages

  a. Save memory
  b. Take a value to perform the next calculation without waiting for all the values ​​to be calculated before starting the next operation - fast
   4. Features of iterators: lazy operation

2. Generator Generator

  An iterator written by yourself is a generator

  Two mechanisms for writing generators (iterators) yourself: generator functions and generator expressions

  1. Generator function --- any function with yield is a generator function

  

def func():
    print('****')
    yield 1
    print('^^^^')
    yield 2 # Record the current position and wait for the next next to trigger the state of the function

g = func()
print('--',next(g))
print('--',next(g))
# The call of the generator function will not trigger the execution of the code, but will return a generator (iterator)
# To execute the generator function, you need to use next
def cloth_g(num):
    for i in range(num):
        yield 'cloth%s'%i


g = cloth_g(1000)
print(next(g))
print(next(g))
print(next(g))

  2. An example of using a generator to monitor file input

import time
def listen_file():
    with open('userinfo') as f:
        while True:
            line = f.readline() #readline does not stop automatically
            if line.strip():
                yield line.strip()
            time.sleep(0.1)

g = listen_file()
for line in g:
    print(line)

  3. send keyword

def func():
    print(11111)
    ret1 = yield 1
    print(22222,'ret1 :')
    ret2 = yield 2
    print(33333,'ret2 :',ret2)
    yield 3

g = func()
ret = next(g)
print (ret)
print(g.send('alex')) # Pass a parameter to the inside of the generator function during the execution of next
print(g.send('Golden Boss'))
# If you want to pass a value in the generator to have an active process, you must use next to trigger the generator for the first time

  Generating Example: Calculating Moving Average

#monthly daily average income
def average():
    sum_money = 0
    day = 0
    avg = 0
    while True:
        money = yield avg
        sum_money += money
        day += 1
        avg = sum_money/day

g = average()
next(g)
print(g.send(200))
print(g.send(300))
print(g.send(600))

  5. Pre-excitation generator

def init(func):
    def inner(*args,**kwargs):
        ret = func (* args, ** kwargs)
        next(ret) # pre-activation
        return right
    return inner

@init
def average():
    sum_money = 0
    day = 0
    avg = 0
    while True:
        money = yield avg
        sum_money += money
        day += 1
        avg = sum_money/day

g = average()
print(g.send(200))
print(g.send(300))
print(g.send(600))

  6、yield from

def generator_func():
    yield from range(5)
    yield from 'hello'
    for i in range(5):
        yield i
    for j in 'hello':
        yield j

g = generator_func()
for i in generator_func():
    print(i)

g1 = function_generator ()
g2 = function_generator ()
next(generator_func())
next(generator_func())

  

  6. How to get the value from the generator The
  first type: next can be stopped at any time. The last error will be reported
  print(next(g))
  print(next(g))
   The second type: the for loop traverses from start to finish without encountering break , return will not stop
   for i in g:
  print(i)
  The third type: the cast of the list tuple data type will load all the data into the memory, which is very wasteful of memory
  print(g)
  print(list(g))

  7. Summary:
  The generator function is a means for our python programmers to implement iterators. The
  main feature is that
  calling a generator function with yield in the function will not execute the code in this function, but will only get a generator (iterator). )
   Only when the value is taken from the generator, the code inside the function will be executed, and each time a piece of data is obtained, the code
  to obtain the data will be executed. The methods of obtaining data include: next send loop data type mandatory conversion
  yield return value A convenient method, if it is an iterable loop, and you want to return each element in the iterable data, you can use yield from
  when using send, you need to pre-excite after the generator is created, you can use decoration in this step Generator Completion
  Features of Generators: Memory-Saving Lazy Operations
  Generators are used to solve memory problems and decoupling between program functions

   8. List Comprehension

  

new_lst = []
for i in range(10):
    new_lst.append(i**2)
print(new_lst)
print([i**2 for i in range(10)])
l = [1,2,3,-5,6,20,-7]
print([i%2 for i in range(10)])
l = [1,2,3,-5,6,20,-7]
print([num for num in l if num%2 == 1])

#30 all numbers divisible by 3
print([i for i in range(30) if i%3 ==0])

The square of all numbers that are divisible by 3 up to #30
print([i**2 for i in range(30) if i%3 ==0])

#find all names in nested lists whose names contain two 'e's
names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
         ['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva']]
print([name for name_lst in names for name in name_lst if name.count('e') == 2])

  9. Generator Expressions

l = [i for i in range(30) if i%3 ==0] # When the list is sorted backwards
g = (i for i in range(30) if i%3 ==0) # Use the generator expression when the generator expression has a huge amount of data
print(l)
print(g)
for i in g:print(i)

  10. Interview questions

def demo():
    for i in range(4):
        yield i

g=demo()

g1=(i for i in g)
g2=(i for i in g1)

print(list(g1))
print(list(g2))

def add(n,i):
    return n+i

def test():
    for i in range(4):
        yield i

g=test()
for n in [1,3,10]:
    g=(add(n,i) for i in g)

print(list(g))


#A generator can only be taken once
#The generator never executes when it doesn't find the value it wants
#When he executes, the values ​​of all variables at the time of execution shall prevail

  

Guess you like

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