20180730 初次上传

20180731 更新,4、列表生成式,以及部分注释

#!/usr/bin/env python
# -*- coding:utf-8 -*- # ********************day19_生成器 *******************
# ********************day19_生成器 *******************
# ********************day19_生成器 ******************* '''
# # 1、yield # # 2、yield举例-->生孩子 # # 3、yiel举例--> 卖包子 # # 4、列表生成式----->不使用yield 举例--> 卖包子
# # # 生成列表,然后加载到内存当中,在进行使用
# # # 缺点1:占空间大
# # # 缺点2:效率低 # # # 4.1、yield-->for三元运算的过程
# # # for三元运算见 人口普查 实例 # # 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 # # https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431779637539089fd627094a43a8a7c77e6102e3a811000
# # 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、yield-->for三元运算的过程
# # # for三元运算见 人口普查 实例
# # #
# #
#
# 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赋值的") # 字符串发送给 yiled,使得,firt = yiled = 字符串
# 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

最新文章

  1. 一个php 字符串判断问题
  2. 移动端页头推荐配置 出现找不到favicon.ico错误原因和解决方法
  3. C#.Net 调用方法,给参数赋值的一种技巧
  4. Java的锁优化
  5. tomcat的部署
  6. SPA与DPA 攻击【转】
  7. When not to automate 什么时候不进行自动化
  8. 我的第一个Struts程序
  9. POJ 3013
  10. SQL ID自增列从1开始重新排序 分类: SQL Server 2014-05-19 14:46 652人阅读 评论(0) 收藏
  11. [Codeforces Round #237 (Div. 2)] A. Valera and X
  12. 网站常用js代码搜集
  13. mybatis 一点整理
  14. 《Head First Java》读书笔记(2) - Java面向对象思想
  15. 如何发起、防御和测试XSS攻击,我们用DVWA来学习(下)
  16. 20172328 2018-2019《Java软件结构与数据结构》第六周学习总结
  17. [转]认识JWT
  18. 个人技术博客Alpha----Android Studio UI学习
  19. OpenSCAD(1)基础教程
  20. winform连接oracle时Oracle.DataAccess.dll版本问题 Silverlight

热门文章

  1. PHP算法之最长公共前缀
  2. robotframework+python3+selenium之创建第一个项目---第三集
  3. typescript + vue开发遇到的坑
  4. Centos7.5 安装sonarqube-7.1
  5. js循环给li绑定事件实现和弹出对应的索引
  6. leetcood学习笔记-235-二叉搜索树的最近公共祖先
  7. 全球首个开放应用模型 OAM 开源
  8. CSS3:CSS3 边框
  9. 7、postman的变量
  10. linux 下 CDH4.5编译