生成器(generator),迭代器(yield)
g=(i for i in range(10)) #小括号表示生成一个迭代生成器。[]是列表生成器
g.__next__()
yield将一个函数变成生成器
import time
def f():
for i in range(10):
yield i #如果换成return i,则最终只能返回0。
print('aa')
m=f() #m是一个generator
print(m)
print(dir(m))
print(m.__next__())
print(m.__next__())
time.sleep(2)
print(m.__next__())
按字节读取文本:
def rb():
f=open('e:\\a.txt','rb')
while True:
size=6 #每次读取6个字节
block=f.read(size)
if block:
yield block
else:
break
f.close()
rr=rb()
for r in rr:
print r
yield中的next()和send():
def yed():
print "enter fun",
yield 10
yy = yed()
yy.next() #返回"enter fun"
print yy.next() #返回 enter fun 10 def yed():
print "enter fun",
yield 10
print "next1"
yield 20
yy = yed()
yy.next()
yy.next()
# #返回enter fun next1 def yed():
print "enter fun",
yield 10
print "next1"
yield 20
yy = yed()
yy.next()
yy.send('hello') #此处send的参数hello被yield 10接收了,但是并没有进行打印,所以不会被打印
#返回enter fun next1 def yed():
print "enter fun",
y1 = yield 10
print "next1",
print y1
yield 20
yy = yed()
yy.next()
yy.send('hello') #send发送的值会成为当前yield的结果,即yield 10,即y1,并且send的返回结果是下一个yield的结果(相当于了next())
#返回 enter fun next1 hello def yed():
print "enter fun",
y1 = yield 10
print "next1",
print y1
y2 = yield 20 yy = yed()
mn = yy.next() #结果为yield 10的参数,即10
ms = yy.send('hello') #结果为yield 20的参数,即20。hello被y1接收到了 print 'mn: %s, ms: %s' %(mn,ms)
#返回enter fun next1 hello
#mn: 10, ms: 20
第一次调用时,要使用next()语句或是send(None),不能使用send发送一个非None的值,否则会出错的,因为没有yield语句来接收这个值。所以第一次时next() == send(None)
send(msg) 和 next()是有返回值的,它们的返回值很特殊,返回的是下一个yield表达式的参数。比如yield 10,则返回10 。
协同处理数据:http://www.tuicool.com/articles/VBfAzaQ
我们现在利用yield关键字会自动冻结函数堆栈的特性,想象一下,假如现在有两个函数f1()和f2(),各自包含yield语句,见下例。主线程先启动f1(), 当f1()执行到yield的时候,暂时返回。这时主线程可以将执行权交给f2(),执行到f2()的yield后,可以再将执行权交给f1(),从而实现了在同一线程中交错执行f1()和f2()。f1()与f2()就是协同执行的程序,故名协程。
import random #消费生成的数据
def consume():
print 'waiting to consume'
while True:
data = yield
sum_data = sum(data)
print '%s was consumed,the sum is %d ' %(str(data),sum_data) #生成数据
def produce():
print 'begin to produce'
consumer = consume()
consumer.next()
while True:
data = random.sample(range(10),3)
print '%s produced' %(str(data))
consumer.send(data)
yield producer = produce()
for i in range(5):
producer.next() #直接执行produce()里面的代码:在produce里面先生成data,然后再将其发送给consume
可以优化为如下:
import random #消费生成的数据
def consume():
print 'waiting to consume'
while True:
data = yield
sum_data = sum(data)
print '%s was consumed,the sum is %d ' %(str(data),sum_data) #生成数据
def produce(consumer):
print 'begin to produce'
while True:
data = random.sample(range(10),3)
print '%s produced' %(str(data))
consumer.send(data)
yield consumer = consume()
producer = produce(consumer)
consumer.next() #打印waiting to consum for i in range(5):
producer.next() #直接执行produce()里面的代码:在produce里面先生成data,然后再将其发送给consume
返回结果如下:
waiting to consume
begin to produce
[2, 9, 3] produced
[2, 9, 3] was consumed,the sum is 14
[1, 5, 2] produced
[1, 5, 2] was consumed,the sum is 8
[8, 5, 4] produced
[8, 5, 4] was consumed,the sum is 17
[6, 1, 2] produced
[6, 1, 2] was consumed,the sum is 9
[8, 6, 3] produced
[8, 6, 3] was consumed,the sum is 17
异步并发????
from time import sleep,ctime def consumer(name):
print '%s want to consume' % name
while True:
bb = yield
print '%s was consumed by %s' %(bb,name) def producer(pname):
c1=consumer('A')
c2=consumer('B')
c1.next()
c2.next()
print '%s begin to produce' % pname for i in range(5):
sleep(1)
print '%s produce 2' % pname
c1.send(i)
c2.send(i) producer('zhangsan')
返回结果如下:
A want to consume
B want to consume
zhangsan begin to produce
zhangsan produce 2
0 was consumed by A
0 was consumed by B
zhangsan produce 2
1 was consumed by A
1 was consumed by B
zhangsan produce 2
2 was consumed by A
2 was consumed by B
zhangsan produce 2
3 was consumed by A
3 was consumed by B
zhangsan produce 2
4 was consumed by A
4 was consumed by B
继续看这个-2016.5.3 http://my.oschina.net/u/877348/blog/184058
生成器(generator),迭代器(yield)的更多相关文章
- Python 生成器与迭代器 yield 案例分析
前几天刚开始看 Python ,后因为项目突然到来,导致Python的学习搁置了几天.然后今天看回Python 发现 Yield 这个忽然想不起是干嘛用的了(所以,好记性不如烂笔头.).然后只能 花点 ...
- Python 生成器 (generator) & 迭代器 (iterator)
python 生成器 & 迭代器 生成器 (generator) 列表生成式 列表生成式用来生成一个列表,虽然写的是表达式,但是储存的是计算出来的结果,因此生成的列表受到内存大小的限制 示例: ...
- Python 第四篇:生成器、迭代器、装饰器、递归函数与正则表达式
一:生成器:Generator,可以理解为是一种一个函数产生一个迭代器,而迭代器里面的数据是可以通过for循环获取的,那么这个函数就是一个生成器,即生成器是有函数生成的,创建生成器使用()表示,比如g ...
- Python(迭代、三元表达式、列表生成、生成器、迭代器)
迭代 什么是迭代 1 重复 2 下次重复一定是基于上一次的结果而来 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). ...
- 10、Python迭代器与生成器(iterator、for循环、generator、yield)
一.迭代器(foreach) 1.可迭代的对象 内置有__iter__方法的都叫可迭代的对象. Python内置str.list.tuple.dict.set.file都是可迭代对象. x = 1._ ...
- ES6中的迭代器(Iterator)和生成器(Generator)
前面的话 用循环语句迭代数据时,必须要初始化一个变量来记录每一次迭代在数据集合中的位置,而在许多编程语言中,已经开始通过程序化的方式用迭代器对象返回迭代过程中集合的每一个元素 迭代器的使用可以极大地简 ...
- Python进阶内容(四)--- 迭代器(Iterator)与生成器(Generator)
迭代器 我们已经知道,可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的ge ...
- python生成器(generator)、迭代器(iterator)、可迭代对象(iterable)区别
三者联系 迭代器(iterator)是一个更抽象的概念,任何对象,如果它的类有next方法(next python3)和__iter__方法返回自己本身,即为迭代器 通常生成器是通过调用一个或多个yi ...
- Python之生成器(generator)和迭代器(Iterator)
generator 生成器generator:一边循环一边计算的机制. 生成器是一个特殊的程序,可以被用于控制循环的迭代行为.python中的生成器是迭代器的一种,使用yield返回值函数,每次调用y ...
- TypeScript 迭代器(iterator)和生成器(generator)
⒈迭代器(iterator) 1.可迭代性 当一个对象实现了Symbol.iterator属性时,我们认为它是可迭代的. 一些内置的类型如 Array,Map,Set,String,Int32Arra ...
随机推荐
- 在Struts2标签s:textfield中显示正确的日期
Java代码 struts2中的日期期输入显示问题 struts2 中的默认的日期输出并不符合我们的中文日常习惯.以下是我知道的在struts2中进行日期格式化输出的几种方式. 1.利用 &l ...
- 开例外!微信小程序登录绕过CAS单点登录(SSO)认证检查
1 为了让微信API能够绕过CAS认证检查,将微信api入口部分设计为独立的模块.放入controller目录下,命名为wechat.java文件为WechatController.java 文件大体 ...
- [codeup] 1943 进制转换
题目描述 将一个长度最多为30位数字的十进制非负整数转换为二进制数输出. 输入 多组数据,每行为一个长度不超过30位的十进制非负整数.(注意是10进制数字的个数可能有30个,而非30bits的整数) ...
- 【转】Js获取当前日期时间及其它操作
Js获取当前日期时间及其它操作 原文地址:http://www.cnblogs.com/carekee/articles/1678041.html var myDate = new Date();my ...
- Linux man C++ 库函数
默认情况下,linux是的man是不能查阅C++的标准库函数的,这个很不方便,那有没有办法可以直接man C++标准库函数呢? 当然有,不过要自己动手,自己动手,才能丰衣足食! 1. 下载安装manp ...
- RabbitMQ.NET In Window Service
工作中要求使用RabbitMQ,以Windows Service 模式启动,中间有遇到一些问题,网上大部分博客有误导倾向, 在这里做一个简单的记录,以免后面的人走坑: 1. 自动重新连接,不需要手动处 ...
- 安装 wordpress 出现 抱歉,我不能写入wp-config.php文件
本文告诉大家如何安装 wordpress ,在安装过程出现 抱歉,我不能写入wp-config.php文件如何解决 下载 wordpress China 简体中文 - WordPress 安装 在安装 ...
- 3 springboot配置文件
springboot 是习惯优于配置,抛弃SSM中繁琐的配置内容,大量的基础配置由框架的完成.所以我们现在没有用任何的配置文件,可以快速的将项目运行起来. 我们找到web模块中的application ...
- 《AlphaGo世纪对决》与周志华《机器学习》观后感
这两天看了<AlphaGo世纪对决>纪录片与南大周志华老师的<机器学习>,想谈谈对人工智能的感想. 首先概述一下视频的内容吧,AlphaGo与李世石对战的过程大家都有基本的了解 ...
- POJ1426(KB1-E 暴力搜索)
Find The Multiple Description Given a positive integer n, write a program to find out a nonzero mu ...