1. Generator function Advanced
# def generator ():
# print (123)
# content = Yield 1
# print (' ======= ', content)
# Print (456)
# arg = yield 2
# ‘‘‘‘‘‘
# yield
# G1 = Generator ()
# g2 = Generator ()
# g1.__next__ ()
# g2.__next__ ()
# print (' * * * ', generator (). __next__ ())
# print (' * * * ', generator (). __next__ ())
# g = Generator ()
# ret = g.__next__ ()
# print (' * * * ', ret)
# ret = g.send (' hello ') #send的效果和next一样
# print (' * * * ', ret)
#send the effect of getting the next value is basically the same as next
#只是在获取下一个值的时候, pass a data to the position of the last yield
#使用send的注意事项
# when using the generator for the first time, use next to get the next value
# The last yield cannot accept external values
# Get moving Averages
# 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)
# Print (AVG1)
#预激生成器的装饰器
# def Init (func): #装饰器
# 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
# def generator ():
# a = ' ABCDE '
# b = ' 12345 '
# for I in A:
# yield I
# for I in B:
# yield I
# def generator ():
# a = ' ABCDE '
# b = ' 12345 '
# yield from a
# yield from B
#
# g = Generator ()
# for I in G:
# Print (i)
# Send
# Send has the same scope as next
# You can't use Send for the first time
# The last yield in the function cannot accept the new value
# Examples of calculating moving averages
# Example of an adorner for a pre-excitation generator
# yield from
2. Generator Expressions
# Lamb
# Egon
# egg_list=[' egg%s '%i for I in range ' #列表推导式
# Print (egg_list)
# egg_list = []
# for I in range (10):
# Egg_list.append (' egg%s '%i)
# Print (egg_list)
# print ([I*i for I in range (10)])
#生成器表达式
# g = (I for I in range (10))
# Print (g)
# for I in G:
# Print (i)
# brackets are not the same
# The value returned is not the same = = = hardly consumes memory
# Old hen = (' egg%s '%i for I in range) #生成器表达式
# print (Old hen)
# for egg in old hen:
# print (egg)
# g = (i*i for I in range (10))
# g.__next__ ()
3. Various derivation formulas
[Each element is either an element-related operation for element in an iterative data type] #遍历之后挨个处理
[an element that satisfies a condition is related to an action for element in an iterative data type if element-related condition] #筛选功能
#30以内所有能被3整除的数
RET = [i-I in range] if i%3 = = 0] #完整的列表推导式
g = (I for I in range () if i%3 = = 0) #完整的列表推导式
Print (ret)
#30以内所有能被3整除的数的平方
RET = [I*i for i in (1,2,3,4) if i%3 = = 0]
ret = (i*i for I in range () if i%3 = = 0)
Print (ret)
# example Three: Find all names in a nested list with two ' E ' names
names = [[' Tom ', ' Billy ', ' Jefferson ', ' Andrew ', ' Wesley ', ' Steven ', ' Joe '],
[' Alice ', ' Jill ', ' Ana ', ' Wendy ', ' Jennifer ', ' Sherry ', ' Eva ']
RET = [name for LST in Names-name in LST if Name.count (' E ') ==2]
ret = (name for LST in Names-name in LST if Name.count (' e ') ==2)
Print (ret)
Dictionary derivation type
Example one: Swap the key and value of a dictionary
Mcase = {' A ': ten, ' B ': 34}
#{10: ' A ', C: ' B '}
Mcase_frequency = {Mcase[k]: K for k in Mcase}
Print (mcase_frequency)
Example two: Merge the value values of the case, unify the K into lowercase
Mcase = {' A ': ten, ' B ':, ' a ': 7, ' Z ': 3}
#{' A ': 10+7, ' B ':, ' Z ': 3}
Mcase_frequency = {k.lower (): Mcase.get (K.lower (), 0) + mcase.get (k.upper (), 0) for K in Mcase}
Print (mcase_frequency)
Set derivation, self-bringing result de-weight function
Squared = {x**2 for x in [1,-1, 2]}
Print (squared)
Various derivations: Generator List Dictionary Collection
Traversal operations
Filtering actions
Python learning 14th day generator function Advanced Generator expression various derivations