Category:python笔记

Article From:https://www.cnblogs.com/chuangming/p/9063369.html

# generator function

# As long as the functions containing the yield keyword are generator functions.

# yield Can't be shared with return and need to be written in the function

# Generator function: after execution, a generator is generated as the return value.

# The generator contains both the __next__ method and the __iter__ method, so the generator is also an iterator, you can use the __next__ method to get the next value or the for loop.

# yieldLike return, you can return a value, but return ends a function directly after the return value, and yield does not.

def generator(): '''It contains yield, which is a generator function''' print(1) yield 'a' ret = generator() print(ret) print(ret.__next__()) ''' <generator object generator at 0x02FCF8D0> 1 a '''

# If I want to generate 2000000 wow, use the generator to generate one for each generator, so that there will be no memory shortage.

def wahaha(): for i in range(2000000): yield 'Wow haha%s' % i g = wahaha() for i in g: print(i)

# send The effect of getting the next value is basically the same as next

# When you get the next value, you pass a data to the location of the previous yield.

# Attention to the use of send

# The first time you use the generator is to use next to get the next value.

# The last yield does not accept external values

def generator(): print(123) content = yield 1 print('=======', content) print(456) arg = yield 2 g = generator() ret = g.__next__() print('***', ret) ret = g.send('hello') # sendThe effect is the same as next print('***', ret) ''' 123 *** 1 ======= hello 456 *** 2 '''

` `

# Get the moving average

# 10 20 30 10

# 10 15 20 17.5

# avg = sum/count

def average(): sum = 0 count = 0 avg = 0 while True: num = yield avg sum += num # 10 count += 1 # 1 avg = sum/count avg_g = average() avg_g.__next__() avg1 = avg_g.send(10) avg1 = avg_g.send(20) avg1 = avg_g.send(30) print(avg1)

`# Decorator for preexcitation generator`

def init(func): # Decorator def inner(*args,**kwargs): g = func(*args,**kwargs) # g = average() g.__next__() return g return inner @init def average(): sum = 0 count = 0 avg = 0 while True: num = yield avg sum += num # 10 count += 1 # 1 avg = sum/count avg_g = average() # ===> inner ret = avg_g.send(10) print(ret) ret = avg_g.send(20) print(ret)

# python 3 New characteristics

# python 2 # def generator(): # a = 'abcde' # b = '12345' # for i in a: # yield i # for i in b: # yield i # python 3 def generator(): a = 'abcde' b = '12345' yield from a yield from b g = generator() for i in g: print(i) ''' a b c d e 1 2 3 4 5 '''

**# Generator Expressions**

# List deduction print([i*i for i in range(10)]) # Generator Expressions print(i for i in range(10)) ''' [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] <generator object <genexpr> at 0x0120FAB0> '''

** # Generator interview**

# Interview question 1

def demo(): for i in range(4): yield i # Generators are inert, and they are only executed when they are executed. g = demo() # Generator object g1 = (i for i in g) g2 = (i for i in g1) print(list(g1)) # list(g1)Take out all the contents of G1 print(list(g2)) # g1After execution, it is empty, so list (G2) is an empty list. ''' [0, 1, 2, 3] [] '''

# Interview question 2

def add(n, i): return n+i def test(): for i in range(4): yield i g = test() for n in [1, 10]: g = (add(n, i) for i in g) # From top to bottom, up until now, the generator has not taken the value. '''Amount toN = 1G = (add (n, I) for I in G)N = 10G = (add (n, I) for I in (add (n, I) for I * (()))HereN = 10, not n = 1, bothG = (add (10, I) for I in (add (10, I) for I in in, 1, 2, V)So, the final result is [20, 21, 2.2, 23]''' print(list(g)) # Here, the generator starts to get the value # [20, 21, 22, 23]

Link of this Article:
generator