Python generator / generator function / derivation / derivation function

1. Generator
  generator is essentially an iterator

  There are three ways in python ⽅ ⽣ generator to obtain:

    1. function generator
    2. The generator is achieved by various derivations ⽣
    3. by switching the data may be acquired generator

  Features generators and iterators the same way as the values and iterators. (__Next __ (), send (): to pass on a yield value).
  Generator is generally created by the generator or function generator expression
  is actually handwriting iterator

2. The generator function
  and a normal function without distinction. Yield there is a function generator function.
  Generator function when executing the default does not perform the function thereof. Returns generator
  through the generator __next __ () Staging this function.
  send () to pass on a yield value, not the beginning of another (not on a yield), the final yield can not send ()

Therefore especially First, we look at a very simple function:

def func():
    print("111")
    return 222
ret = func()
print(ret)
结果: 111 222

 

The return of the function generator is changed to yield

def func():
    print("111")
    yield 222
ret = func()
print(ret)
结果:
<generator object func at 0x10567ff68>

  When running a result of the same and the screen does not. Why. Since the function exists in the yield. So this is ⼀ a function generator function. This time we again perform the function of time. It is no longer a function of the execution. ⽽ is to get the builder. how to use it? want an iterator. essence generator is an iterator, so we can direct perform __next __ () is executed

The following generator:

FUNC DEF (): 
    Print ( "111") 
yield 222 
. Gener = FUNC () function is not executed at this time # ⽽ generator is obtained 
ret = gener .__ next __ () # This function is only performed when the effect of yield. ., and are returned as return data 
print (ret) 
results: 
111 
222

 

So we can see, yield and return the effect is the same. What difference does it make? Yield is a function of the segment to Perform line. Return it? Perform direct stop function.

def func():
    print("111")
    yield 222
    print("333")
    yield 444
gener = func()
ret = gener.__next__()
print(ret)
ret2 = gener.__next__()
print(ret2)
ret3 = gener.__next__() # 最后⼀个yield执⾏完毕. 再次__next__()程序报错, 也就是说. 和return⽆关了.
print(ret3)
结果:
111
Traceback (most recent call last): 222
333
  File "/Users/sylar/PycharmProjects/oldboy/iterator.py", line 55, in
<module>
444
ret3 = gener.__next__() # 最后一个yield执行完毕. 再次__next__()程序报错, 也就是说. 和return⽆关了.
StopIteration

当程序运⾏完最后一个yield. 那么后⾯继续进行__next__()程序会报错.

我们来看send⽅方法, send和__next__()⼀一样都可以让⽣生成器执⾏行行到下⼀一个yield.

def eat(): 
    print("我喜欢玩王者荣耀的:") 
    a = yield "鲁班" 
    print("a=",a)
    b = yield "程咬金" 
    print("b=",b)
    c = yield "安琪拉" 
    print("c=",c) 
    yield "GAME OVER"

gen = eat() # 获取⽣成器
ret1 = gen.__next__() 
print(ret1)
ret2 = gen.send("大乔") 
print(ret2)
ret3 = gen.send("后裔) 
print(ret3)
ret4 = gen.send("马克") 
print(ret4)

send和__next__()区别:

1. send和next()都是让⽣成器向下走一次
2. send可以给上一个yield的位置传递值,不能给最后一个yield发送值.在第一次执⾏⽣成器代码的时候不能使用send()

⽣成器可以使⽤for循环来循环获取内部的元素:

def func():
    print(111)
    yield 222
    print(333)
    yield 444
    print(555)
    yield 666
gen = func()
for i in gen:
  print(i)
结果: 111 222 333 444 555 666

3. 推导式
  1. 列表推导式 [结果 for循环 条件筛选]\

首先我们先看一下这样的代码, 给出一个列列表, 通过循环, 向列表中添加1-14 :

lst = []
for i in range(1, 15):
    lst.append(i)
print(lst)

替换成列列表推导式:

lst = [i for i in range(1, 15)]
print(lst)

列表推导式是通过⼀行来构建你要的列表, 列表推导式看起来代码简单. 但是出现错误之后很难排查.

筛选模式:
  [ 结果 for 变量量 in 可迭代对象 if 条件 ]

# 获取1-100内所有的偶数
lst = [i for i in range(1, 100) if i % 2 == 0] 
print(lst)

⽣成器表达式和列表推导式的语法基本上是一样的. 只是把[]替换成()

gen = (i for i in range(10))
    print(gen)
结果:
<generator object <genexpr> at 0x106768f10>

⽣成器表达式也可以进行筛选:

# 获取1-100内能被3整除的数
gen = (i for i in range(1,100) if i % 3 == 0) 
for num in gen:
    print(num)

  2. 字典推导式 {k:v for循环 条件筛选}

# [11,22,33,44] => {0:11,1:22,2:33,3:44}
lst = [11,22,33,44]
dic = {i:lst[i] for i in range(len(lst)) if i < 2}
    print(dic)
# 语法:{k:v for循环 条件筛选}

  3. 集合推导式 {k for循环 条件}

 

# Set of push-type 
LST = [1,1,4,6,7,7,4,2,2 ] 
S = {EL for EL in LST}
 Print (S) 
S = SET (LST)
 Print (S)

 

4. generator expression

  ⽣ list comprehensions and generator expressions of difference:

  1. When a list of memory consumption derived comparator disposable loading. Generator Using expression accounting for almost no memory. Using only the allocated memory and Use

  2. The value obtained is not ⼀ comp. Formula derived list is a list of columns obtained. ⽣ generator expression obtained is a generator.

  (Results for cycling conditions)
  Features:
    1. Inert mechanism
    2. Only the forward
    3. save memory

Guess you like

Origin www.cnblogs.com/shagudi/p/10962976.html