生成器表达式:
#列表推导式 # y = [1,2,3,4,5,6,7,8] # x = [1,4,9,16,25,36,49,64] # x = [] # for i in y: # x.append(i*i) # print(x) # x = [i*i for i in y] # print(x) #range(100) # x2 = [i/2 for i in range(100)] # print(x2) #生成器表达式 # x = [i*i for i in y] # print(x) # g = (i*i for i in y) # print(g) # print(list(g)) # for i in g: # print(i) # # l = ['鸡蛋%s'%i for i in range(10)] # print(l) # laomuji = ('鸡蛋%s'%i for i in range(10)) # for egg in laomuji: # print(egg)
生成器函数:
1 # def cloth(): 2 # for i in range(100): 3 # yield '衣服%s'%i 4 # 5 # g = cloth() 6 # for i in g: 7 # print(i) 8 9 # for i in range(100): 10 # print(g.__next__()) 11 # 12 # for i in range(50): 13 # print(g.__next__()) 14 15 #监听文件末尾追加的例子 16 # def tail(): 17 # f = open('文件','r',encoding='utf-8') 18 # f.seek(0,2) 19 # while True: 20 # line = f.readline() 21 # if line: 22 # yield line 23 # import time 24 # time.sleep(0.1) 25 # g = tail() 26 # for i in g: 27 # print(i.strip()) 28 29 # send 30 def func(): 31 print('*'*10) 32 a = yield 5 33 print('a : ',a) 34 yield 10 35 # g = func() 36 # num = g.__next__() 37 # # print(num) 38 # num2 = g.send('alex') 39 # num2 = g.send('aaaa') 40 # print(num2) 41 42 #从哪一个yield开始接着执行,就把一个值传给了那个yield 43 #send不能用在第一个触发生成器 44 #生成器函数中有多少个yield就必须有多少个next+send 45 46 def averager(): 47 total = 0.0 48 count = 0 49 average = None 50 while True: 51 term = yield average 52 total += term 53 count += 1 54 average = total/count 55 # 56 # g_avg = averager() 57 # g_avg.__next__() 58 # print(g_avg.send(10)) 59 # print(g_avg.send(30)) 60 # print(g_avg.send(20)) 61 # print(g_avg.send(100)) 62 # print(g_avg.send(200)) 63 def init(func): #生成器的预激装饰器 64 def inner(*args,**kwargs): 65 g = func(*args,**kwargs) #func = averager 66 g.__next__() 67 return g 68 return inner 69 70 @init 71 def averager(): 72 total = 0.0 73 count = 0 74 average = None 75 term = yield average 76 total += term 77 count += 1 78 average = total/count 79 yield average 80 81 # g_avg = averager() 82 # print(g_avg.send(10)) 83 # print(g_avg.send(30)) 84 85 86 #生成器函数 讲完了 87 #next 88 #进阶 send 89 90 #网络编程 : ftp 91 def func(): 92 a = 'AB' 93 b = 'CD' 94 yield from a 95 # for i in a:yield i 96 yield from b 97 # for i in b:yield i 98 99 'A','B','C','D' 100 #返回了4次 101 g = func() 102 # for i in g: 103 # print(i) 104 105 106 #生成器函数:生成一个生成器的函数 107 #生成器的本质参数迭代器 108 #生成器函数的特点: 109 # 带有yield关键字 110 # 且调用之后,函数内的代码不执行 111 112 #触发执行的方式: 113 #next 114 #send (选会) :send(None) == __next__(),send在next的基础上传一个值到生成器函数内部 115 #send操作不能用在生成器使用的第一次 116 #for循环 117 118 def cloth(): 119 for i in range(100): 120 yield '衣服%s'%i 121 122 g1 = cloth() 123 g2 = cloth() 124 print(g.__next__()) 125 print(g.__next__()) 126 # for c in cloth(): 127 # print(c) 128 # print(g.__next__()) 129 # for i in range(10): 130 # print(g.__next__()) 131 # for i in range(100): 132 # print(g.__next__()) 133 # for c in g: 134 # print(c) 135 # if c.endswith('80'): 136 # break 137 # print(g.__next__()) 138 # print('*'*20) 139 # for c in g: 140 # print(c) 141 142 143 # g.__next__() #== next(g) 144 # g.__iter__() #== iter(g) 145 #内置函数
推导式拓展:
# new_l = [] # for i in range(30): # if i%3 == 0: # new_l.append(i) # print(new_l) # # print(list(range(0,30,3))) # print([i for i in range(30) if i%3 == 0]) # [i*i for i in range(30) if i%3 == 0] # def square(x): # return x*x # [square(i) for i in range(30) if i%3 == 0] # names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'], # ['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva']] # ret = (name for name_lst in names for name in name_lst if name.count('e') >= 2) # print(ret) # 不利于代码可读,但是代码简洁 # mcase = {'a': 10, 'b': 34} # mcase_frequency = {mcase[k]:k for k in mcase} # print(mcase_frequency) # mcase = {'a': 10, 'b': 34, 'A': 7, 'Z': 3} # mcase_frequency = {k.lower(): mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0) for k in mcase} # print(mcase_frequency) squared = {x**2 for x in [1, -1, 2]} #{1,4} print(squared) # Output: set([1, 4]) #考试 #2:00 --> 敲敲代码 #明天 : # 装饰器剩余的两个例子 # 今天的考试内容明天要讲 #明天的默写: #[i/2 for i in range(100)] #[i*i for i in range(30) if i % 3 is 0] # 修改成生成器表达式,且要求讲使用方法默写出来