day19_生成器

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# ********************day19_生成器 *******************
# ********************day19_生成器 *******************
# ********************day19_生成器 *******************

'''
# # 1、yield

# # 2、yield举例-->生孩子

# # 3、yiel举例-->  卖包子

# # 4、不使用yield 举例-->  卖包子
# # # 生成列表,然后加载到内存当中,在进行使用
# # # 缺点1:占空间大
# # # 缺点2:效率低

# # 5、yield 举例-->人口普查
# # # 对所有的人口进行总和等计算


# # 6、、yield 对文件
# # # 取出来的是字符串

# # 7、不同变量对yield所在函数多次取值
# # # 某一变量 对yield的所在的生成器取完所有的值,定义一个新的变量,可以对生成器再次取值
# #

# # 8、不同变量,对生成器,同时取值互不影响

# # 9、send 迭代器中的方法
# # # yield 3相当于return控制的是函数的返回值,这里返回3
# # # x = yiled的另一个特性是:接受send传过来的值,赋值给x
# # # next方法的过程:  t.__next__()(第一次next) --> 拿到yield的返回值(如 yield 1 等同于 return1) -->保存当前状态-->
# # #             -->   t.__next__()(第2次next) --> 拿到yield的返回值(如 yield 1 等同于 return1) -->保存当前状态-->
# # #             -->...-->迭代结束
# # # send()方法的过程:t.send(22)(第一次send)--> 将11赋值个yield左边的等式(如 firt = yield, firt得到的值是22 ) -->
# # #             --> 执行下面的程序,直到下一次的yield之前-->保存运行位置的状态 -->重复上述过程--> 。。。-->结束
# #


# # 10、单线程单触发
# # #  这种方式,执行效率低下,可与下面的“单线程多触发对比”

# # 11、迭代器实现单线程多触发
# # # 功能说明:一边做包子,一边把做好的包子拿给别人吃
# # # 可以同时执行触发多个程序,


'''

# print("分割线".center(80,'-'))
# --------------------------------------分割线---------------------------------------
# --------------------------------------分割线---------------------------------------
# --------------------------------------分割线---------------------------------------

# 01
# 01
# 01

# # 1、yield
#
# def test():
#     yield 1
#     yield 2
#     yield 3
#     yield "没有了,再来就报错了"
# res = test()
# print("迭代器地址:",res)
# print("第一个:",res.__next__())
# print("第2 个:",res.__next__())
# print("第3 个:",res.__next__())
# print("第4 个:",res.__next__())
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# # 迭代器地址: <generator object test at 0x00000000039AFA40>
# # 第一个: 1
# # 第2 个: 2
# # 第3 个: 3
# # 第4 个: 没有了,再来就报错了
# #
# # Process finished with exit code 0



# # 2、yield举例-->生孩子
# #
# import time
# def test():
#     print("开始生孩子啦...")
#     print("开始生孩子啦...")
#     print("开始生孩子啦...")
#     yield "me"          # return
#     time.sleep(3)
#
#     print("开始生'son'啦...")
#     yield "son"
#     time.sleep(3)
#
#     print("开始生'Sunzi'啦...")
#     yield "SunZi"
#     time.sleep(3)
#
#     yield "没有了,再来就报错了"
#
# res = test()
# print("迭代器地址:",res)
# print("第一个:",res.__next__())
# print("第2 个:",res.__next__())
# print("第3 个:",res.__next__())
# print("第4 个:",res.__next__())
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# # 迭代器地址: <generator object test at 0x000000000399FA40>
# # 开始生孩子啦...
# # 开始生孩子啦...
# # 开始生孩子啦...
# # 第一个: me
# # 开始生'son'啦...
# # 第2 个: son
# # 开始生'Sunzi'啦...
# # 第3 个: SunZi
# # 第4 个: 没有了,再来就报错了
# #
# # Process finished with exit code 0



# # 3、yiel举例-->  卖包子
# # # 使用yield 的好处就是,yield每次只迭代一个值,同时不再用内存,可以在执行某个功能回来后,
# # # 从之前的位置,继续向下取值
# #
#
# def product_bun():
#     for i in range(100):
#         print("正在生产包子")
#         yield("一提包子 %s出来啦!"%i)
#         print("卖包子")
#
# pro_b = product_bun()
#
# bun1 = pro_b.__next__()
# print("看-->",bun1)
# # 代码实现某个特定功能
# print("=======做某事,回来后再继续吃包子=========")
# bun2 = pro_b.__next__()
# print("看-->",bun2)
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# # 正在生产包子
# # 看--> 一提包子 0出来啦!
# # =======做某事后,回来再继续吃包子=========
# # 卖包子
# # 正在生产包子
# # 看--> 一提包子 1出来啦!
# #
# # Process finished with exit code 0


# # 4、不使用yield 举例-->  卖包子
# # # 生成列表,然后加载到内存当中,在进行使用
# # # 缺点1:占空间大
# # # 缺点2:效率低
# #
#
# def product_bun():
#     ret = []
#     for i in range(5):
#         # print("正在生产包子")
#         ret.append("包子第  %s 盘出来啦!"%i)
#         # print("卖包子")
#     return ret
#
# bun_l = product_bun()
# print(bun_l)
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# # ['包子第  0 盘出来啦!', '包子第  1 盘出来啦!', '包子第  2 盘出来啦!', '包子第  3 盘出来啦!', '包子第  4 盘出来啦!']
# #
# # Process finished with exit code 0






# 03
# 03
# 03
# # # 4.1、生成列表,然后加载到内存当中,在进行使用
# # # 缺点1:占空间大
# # # 缺点2:效率低
# #
#
# def xiadan():
#     for i in range(3):
#         yield '鸡蛋%s' %i
# alex_lmj = xiadan()
# print(alex_lmj.__next__())
# print(alex_lmj.__next__())
# print(alex_lmj.__next__())
# # print(alex_lmj.__next__())     # StopIteration




# # 5、yield 举例-->人口普查
#  # # 对所有的人口进行总和等计算
# #
# # # 文件:人口普查.txt
'''
{'name':'北京','population':1}
{'name':'山东','population':2}
{'name':'山西','population':3}
{'name':'河北','population':4}
{'name':'台湾','population':5}
'''
#
# def get_population():
#     with open('人口普查','r',encoding='utf-8') as f:
#         for i in f:
#             yield i
# g = get_population()
# print(g)
#
# all_pop = sum( eval(i)['population'] for i in g)
# print(all_pop)
#
# D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# <generator object get_population at 0x000000000399FAF0>
# 15
#
# Process finished with exit code 0




# # 6、、yield 对文件
# # # 取出来的是字符串
# #
# # # 文件:人口普查.txt
'''
{'name':'北京','population':1}
{'name':'山东','population':2}
{'name':'山西','population':3}
{'name':'河北','population':4}
{'name':'台湾','population':5}
'''
#
# def get_population():
#     with open('人口普查','r',encoding='utf-8') as f:
#         for i in f:
#             yield i
# g = get_population()
# print(g)
# g1 = g.__next__()            # 注意: 取出来的值是字符串
# print( g1,type(g1))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# # <generator object get_population at 0x000000000399FAF0>
# # {'name':'北京','population':1}
# #  <class 'str'>
# #
# # Process finished with exit code 0
# #
# #
# #



#
# # 7、不同变量对yield所在函数多次取值
# # # 某一变量 对yield的所在的生成器取完所有的值,定义一个新的变量,可以对生成器再次取值
# #
# # # 文件:人口普查.txt
'''
{'name':'北京','population':1}
{'name':'山东','population':2}
{'name':'山西','population':3}
{'name':'河北','population':4}
{'name':'台湾','population':5}
'''
# def get_population():
#     with open('人口普查','r',encoding='utf-8') as f:
#         for i in f:
#             yield i
# g = get_population()
# print(g)
# all_pop = sum( eval(i)['population'] for i in g)
# print("总人口数:",all_pop)
# # print(g,g.__next__())             # StopIteration表示值已经取完了
#
# m = get_population()                # 可以对它再起取值 不影响
# print(m,m.__next__())
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# # <generator object get_population at 0x000000000399FAF0>
# # 总人口数: 15
# # <generator object get_population at 0x000000000399FCA8> {'name':'北京','population':1}
# #
# #
# # Process finished with exit code 0






# # 8、、不同变量,对生成器,同时取值互不影响
# #
# # # 文件:人口普查.txt
'''
{'name':'北京','population':1}
{'name':'山东','population':2}
{'name':'山西','population':3}
{'name':'河北','population':4}
{'name':'台湾','population':5}
'''
# def get_population():
#     with open('人口普查','r',encoding='utf-8') as f:
#         for i in f:
#             yield i
# g = get_population()
# m = get_population()                # 不同变量,对生成器,同时取值
#
# print('g地址: ',g)
# print('m地址: ',m)
# print('g.__next__()',g.__next__())             # 不同变量,对生成器,同时取值互不影响
# print('m.__next__()',m.__next__())
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# # g地址:  <generator object get_population at 0x000000000399FAF0>
# # m地址:  <generator object get_population at 0x000000000399FCA8>
# # g.__next__() {'name':'北京','population':1}
# #
# # m.__next__() {'name':'北京','population':1}
# #
# #
# # Process finished with exit code 0
#


# 05
# 05
# 05


#
# # 9、send 迭代器中的方法
# # # yield 3相当于return控制的是函数的返回值,这里返回3
# # # x = yiled的另一个特性是:接受send传过来的值,赋值给x
# # # next方法的过程:  t.__next__()(第一次next) --> 拿到yield的返回值(如 yield 1 等同于 return1) -->保存当前状态-->
# # #             -->   t.__next__()(第2次next) --> 拿到yield的返回值(如 yield 1 等同于 return1) -->保存当前状态-->
# # #             -->...-->迭代结束
# # # send()方法的过程:t.send(22)(第一次send)--> 将11赋值个yield左边的等式(如 firt = yield, firt得到的值是22 ) -->
# # #             --> 执行下面的程序,直到下一次的yield之前-->保存运行位置的状态 -->重复上述过程--> 。。。-->结束
# #
#
# def test():
#     print("开始啦……")
#     firt = yield 1              # 相当于,reutrn 1,  firt =None, 运行状态保留在这里
#     print("第一次,yield之后",firt)
#
#     yield 2
#     print("第2次", )
#
# t = test()                  #  这里并没有运行
# res = t.__next__()          # next()内置函数方法也可以
# print("第一次调用next:",res)
#
# res1 =t.send("函数停留在first那个位置,我就是给first赋值的")
# print("第一次调用send:",res1)
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# # 开始啦……
# # 第一次调用next: 1
# # 第一次,yield之后 函数停留在first那个位置,我就是给first赋值的
# # 第一次调用send: 2
# #
# # Process finished with exit code 0







# # 10、单线程单触发
# # #  这种方式,执行效率低下,可与下面的“单线程多触发对比”
# import time
# def produce():
#     ret = []
#     for i in range(3):
#         time.sleep(1)
#         ret.append('包子%s'%i)
#     return ret
#
# def consumer(res):
#     for index, baozi in enumerate(res):
#         time.sleep(1)
#         print('第%s个人,吃了%s'%(index,baozi))
# print("开始点包子啦……")
# res = produce()
# print("开吃咯……")
# consumer(res)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# # 开始点包子啦……
# # 开吃咯……
# # 第0个人,吃了包子0
# # 第1个人,吃了包子1
# # 第2个人,吃了包子2
# #
# # Process finished with exit code 0





# # 11、迭代器实现单线程多触发
# # # 功能说明:一边做包子,一边把做好的包子拿给别人吃
# # # 可以同时执行触发多个程序,
# #
#
# import time
#
# def consumer(name):
#     print("我是【%s】,我准备开始吃包子啦", name)
#     while True:
#         bun = yield
#         time.sleep(1)
#         print("%s 很开心的把【%s】吃掉啦"%(name,bun) )
#
# def producer():
#     c1 = consumer("--wu--")
#     c2 = consumer("  sb  ")
#     c1.__next__()               # 开始运行迭代器
#     c2.__next__()
#     for i in range(3):
#         time.sleep(1)
#         c1.send("包子 %s" %i)
#         c2.send("包子 %s" %i)
#
# producer()
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py
# # 我是【%s】,我准备开始吃包子啦 --wu--
# # 我是【%s】,我准备开始吃包子啦   sb
# # --wu-- 很开心的把【包子 0】吃掉啦
# #   sb   很开心的把【包子 0】吃掉啦
# # --wu-- 很开心的把【包子 1】吃掉啦
# #   sb   很开心的把【包子 1】吃掉啦
# # --wu-- 很开心的把【包子 2】吃掉啦
# #   sb   很开心的把【包子 2】吃掉啦
# #
# # Process finished with exit code 0




猜你喜欢

转载自blog.csdn.net/xu380393916/article/details/81288695