1. 生成器和生成器函数
生成器的本质就是迭代器
生成器的三种创建办法:
1.通过生成器函数
2.通过生成器表达式创建生成器
3.通过数据转换
# def func(): # print("我是周杰伦") # yield "昆凌" # 函数中包含了yield, 当前这个函数就不再是普通的函数了. 是生成器函数 # print("我是王力宏") # yield "李云迪???" # print("我是笛卡尔积") # yield "笛卡尔积是谁" # print("你好啊") # 最后一个yield之后如果再进行__next__() 会报错 # g = func() # print(g.__next__()) # print(func().__next__()) # # g1 = func() # g2 = func() # print(g1.__next__()) # print(g1.__next__()) # # print("==============") # print(g2.__next__())
生成器函数:
函数中包含了yield的就是生成器函数
注意:生成器函数被执行. 获取到的是生成器. 而不是函数的执行
生成器表达式:
(结果 for 变量 in 可迭代对象 if 筛选)
取值:
1. __next__()
2. send(值) 给上一个yield位置传一个值, 第一个和最后一个yield不用传值
3. 可以for循环
4. list(g)
# g = func() # 通过函数func()来创建一个生成器 # print(g.__next__()) # 周杰伦 # print(g.__next__()) # 王力宏 # print(g.__next__()) # 笛卡尔积 # print(g.__next__()) # return 直接返回结果. 结束函数的调用 # yield 返回结果.可以让函数分段执行 # # def func(): # lst = [] # for i in range(1,100001): # lst.append("衣服%s" % i) # return lst # # def gen(): # i = 1 # while i < 100001: # yield "衣服%s" % i # i = i + 1 # g = gen() # print(g.__next__()) # print(g.__next__()) # print(g.__next__()) # print(g.__next__()) # print(g.__next__()) # print(g.__next__()) # # def func(): # yield 11 # yield 22 # yield 33 # yield 44
# g = func() # 拿到的是生成器. 生成器的本质是迭代器. 迭代器可以被迭代 生成器可以直接for循环 # # for i in g: # print(i) # 本质上执行的是__next__() # # it = g.__iter__() # while True: # try: # print(it.__next__()) # except StopIteration: # break
2. 各种推倒式和生成器表达式
1. 列表推倒式 [结果 for 变量 in 可迭代对象 if 筛选]
# 生成列表 里面装1-14的数据 # lst = [] # for i in range(1,15): # lst.append("python%s" % i) # print(lst) # 列表推倒式; 最终给你的是列表 # 语法 [最终结果(变量) for 变量 in 可迭代对象] # lst = [i for i in range(1,15)] # print(lst) # [最终结果 for 变量 in 可迭代对象 if 条件] lst = [i for i in range(1,101) if i%2==0] print(lst) # 1. 获取1-100内能被3整除的数 # lst = [i for i in range(1,101) if i % 3 == 0] # 2. 100以内能被3整除的数的平方 # lst = [i*i for i in range(1,101) if i % 3 == 0] # 3. 寻找名字中带有两个e的⼈的名字 # names = [['Tom', 'Billy', 'Jefferson' , 'Andrew' , 'Wesley' , 'Steven' , # 'Joe'],['Alice', 'Jill' , 'Ana', 'Wendy', 'Jennifer', 'Sherry' , 'Eva']] # lst = [name for first in names for name in first if name.count("e") == 2] # print(lst) # # lst = ["衣服%s" % i for i in range(10000)]
2. 字典推倒式 {结果 for 变量 in 可迭代对象 if 筛选} 结果=>key:value
# g = (i for i in range(10)) # print(list(g)) # gen = ("麻花藤我第%s次爱你" % i for i in range(10)) # for i in gen: # print(i) # 生成器的惰性机制 # def func(): # print(111) # yield 222 # g = func() # g1 = (i for i in g) # g2 = (i for i in g1) # # print(list(g)) # print(list(g1)) # print(list(g2))
# dic = {"a":"b", "c":"d"} # # 把字典中的key:value互换 .{"b":"a", "d":"c"} # new_dic = {dic[key]:key for key in dic} # print(new_dic) # lst1 = ["alex", "wusir", "taibai", "ritian"] # lst2 = ['sb', "很色", "很白", "很牛"] # # {"alex":"sb", "wusir":"很色"} # # dic = { lst1[i]:lst2[i] for i in range(len(lst1))} # print(dic) # dic = {"a":"b","c":"d"} # #把字典中的key:value互换.{"b":"a":"d":"c"} # new_dic = {dic[key]:key for key in dic} # print(new_dic) # # #lst1 = ["alex","wusir","taibai","ritian"] # #lst2 = ['sb',"很色","很白","很牛"] # #{"alex":"sb","wusir":"很色"} # dic = {lst1[1]:lst2[i] for i in range(len(lst1))} # print(dic) # # dic = {"a":"b","c":"d"} # name = "aleX leNb" # e1 = name.find("e", 0,5) # print(e1) # # e2 = name.find("e",5) # print(e2) # count = 1 # while count <= len(name): # if name[count] =='e': # print(count) # count = count + 1 # # s = "123a4b5c" # s = "asdfer" # for c in s: #c :chartor # print(c) # content = input("请输入内容") # lst = content.split("+") # s1 = lst[0] # s2 = lst[1] # a1 = int(s1) # a2 = int(s2) # print(a1+a2) # # lst = ["皇阿玛", "皇额娘", "容嬷嬷", "紫薇"] # it = lst.__iter__() # while True: # try: # name = it.__next__() # print(name) # except StopIteration: # break # # # dic = {"a":"b", "c":"d"} # new_dic = {dic[key]:key for key in dic} #字典的key和value对调 # print(new_dic) # # # lst1 = ["alex","wusir","taibai","ritian"] # lst2 = ['sb',"很色","很白","很牛"] # dic = {lst1[i]:lst2[i] for i in range(len(lst1))} # print(dic) # #列表1和列表二合并,1作为key,2作为value # def func(): # print("111") # return 222 # ret = func() # print(ret) # def func(): # print("111") # yield 222 # ret = func() # print(ret) g = (i for i in range(10)) print(list(g))
3. 集合推倒式 {结果 for 变量 in 可迭代对象 if 筛选} 结果=>key
lst = ["马化腾", "马化腾", "王建忠", "张建忠", "张建忠", "张雪峰", "张雪峰"] s = {i for i in lst} # 集合推倒式 print(s)
def add(a, b): return a + b def test(): for i in range(4): yield i g = test()
def func(): name = "alex" def inner(): print(name) inner() print(inner.__closure__) func()
def add(a, b): return a + b def gen(): for r_i in range(4): yield r_i g = gen() for n in [2, 10]: g = (add(n, i) for i in g) print(list(g))