Python 3. 里filter与generator expression的区别
# -*- coding: utf-8 -*-
"""
A test to show the difference between filter and genrator expression As I believe, generator expression will delay the evaluation of the expression behind if 测试filter与generator expression的区别
根据下面的测试结果,我推测生成式表达式(generator expression)会对if表达式推迟赋值 """ def _odd_iter():
n = 1
while True:
n = n + 2
yield n def _not_divisible(n):
return lambda x: x % n > 0 # 下面四种写法,只有第一种是正确的,其他写法primes0,primes1,primes2,都不会正确筛掉非质数。
def primes():
yield 2
it = _odd_iter() # 初始序列
while True:
n = next(it) # 返回序列的第一个数
yield n
it = filter(_not_divisible(n), it) # 因为为了完成_not_divisible的调用,n的值已经传进来了。
#据我理解,这个结果是 ...filter((lambda x: x % 7 >0), filter((lambda x: x % 5 >0), filter((lambda x: x % 3 >0), _odd_iter()))) def primes0():
yield 2
it = _odd_iter() # 初始序列
while True:
n = next(it) # 返回序列的第一个数
yield n
it = filter((lambda x: x % n >0), it) # 此时n的值未传进来;传进来的是n这个变量
#据我理解,这个结果是 ...filter((lambda x: x % n >0), filter((lambda x: x % n >0), filter((lambda x: x % n >0), _odd_iter()))) def primes1():
yield 2
it = _odd_iter() # 初始序列
while True:
n = next(it) # 返回序列的第一个数
yield n
it = (item for item in it if _not_divisible(n)(item)) # generator这种写法可能把 if 后面的运算都延迟了?
#据我理解,从最后结果反推出,这个结果是 ...(item for item in (item for item in (item for item in _odd_iter() if _not_divisible(n)(item)) if _not_divisible(n)(item)) if _not_divisible(n)(item))
#这里并没有把n的值代进去,而是保留了对n的引用,
#如果我的理解是对的,那么filter跟generator expression是不一样的,generator expression对if后面的表达式是延迟计算的。 def primes2():
yield 2
it = _odd_iter() # 初始序列
while True:
n = next(it) # 返回序列的第一个数
yield n
it = (item for item in it if (lambda x: x % n > 0)(item)) # 结果同primes1
################ test output
print("output by primes:")
for n in primes():
if n < 20:
print(n)
else:
break
print("output by primes0:")
for n in primes0():
if n < 20:
print(n)
else:
break
print("output by primes1:")
for n in primes1():
if n < 20:
print(n)
else:
break
print("output by primes2:")
for n in primes2():
if n < 20:
print(n)
else:
break
基于廖雪峰python 3教程里的讨论:
http://www.liaoxuefeng.com/discuss/001409195742008d822b26cf3de46aea14f2b7378a1ba91000/001480687071611fab98c8f381a4b63a1a94132134c432d000?page=1
14:49:47 2016-12-03
Python 3. 里filter与generator expression的区别的更多相关文章
- Python中Lambda, filter, reduce and map 的区别
Lambda, filter, reduce and map Lambda Operator Some like it, others hate it and many are afraid of t ...
- 详解Python中的生成器表达式(generator expression)
介绍 1.生成器表达式(generator expression)也叫生成器推导式或生成器解析式,用法与列表推导式非常相似,在形式上生成器推导式使用圆括号(parentheses)作为定界符,而不是列 ...
- Python 列表解析list comprehension和生成表达式generator expression
如果想通过操作和处理一个序列(或其他的可迭代对象)来创建一个新的列表时可以使用列表解析(List comprehensions)和生成表达式(generator expression) (1)list ...
- Python高级编程之生成器(Generator)与coroutine(一):Generator
转载请注明出处:点我 这是一系列的文章,会从基础开始一步步的介绍Python中的Generator以及coroutine(协程)(主要是介绍coroutine),并且详细的讲述了Python中coro ...
- list comprehension & generator expression
List comprehensions(列表推导式) are better when you want to iterate over something multiple times. Howeve ...
- django1.11 启动错误:Generator expression must be parenthesized
错误信息: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0 ...
- python3.7环境下创建app、运行Django1.11版本项目报错Generator expression must be parenthesized
有些同学喜欢追求新鲜感~但追求新鲜感终归是要付出一点点代价的 在编程领域有一句至理名言:用东西不要用最新的! 就像每次苹果系统的升级都会有相当一部分用户的手机成砖一样 下面我们就介绍一个因版本升级带来 ...
- ORM基础3 在python脚本里调用Django环境
1.查询 1.# all获取所有的object,结果QuerySet,列表 print('all'.center(80, '=')) ret = models.Person.objects.all() ...
- django 启动错误:Generator expression must be parenthesized 错误信息:
错误为: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x ...
随机推荐
- 黑客是怎样绕过WAF之三重防护绕过讲解
什么是WAF Web Application Firewall 通过执行一系列针对HTTP/HTTPS的安全策略来防御对Web应用的攻击. 目前主要有单设备WAF与云WAF WAF的现状 1.太多数W ...
- 周末“干活”之 Mesos Meetup
周末两天都是大雾霾天,作为运营也不能在家宅,告别了技术就得腿儿勤点儿. 非常感谢 Linker 的 Sam Chen 和 数人科技 的 CTO 共同组织的Mesos Meetup,OneAPM 最帅的 ...
- Android 共享文件的 Runtime 权限
在开发 Android 应用时,总会涉及到获取打电话.地理位置.网络等敏感的用户信息的权限,在 Android 中,联系人.当前位置等这些敏感信息都是由 permissions 保护的,Android ...
- NSBundle的使用,注意mainBundle和Custom Bundle的区别
1.[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器 Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样: NSString *earth ...
- Ubuntu 14.04数据库服务器--mysql的安装和配置
mysql是Oracle公司的一种开放源代码的关系型数据库管理系统,被广泛应用于各中小网站,是一种跨平台的数据库管理系统,现在介绍一下如何在Ubuntu 14.04上安装和配置mysql 1. 更新源 ...
- 乱序双发射 和 GHB的分支预测
乱序执行(out-of-order execution)是指CPU采用了允许将多条指令不按程序规定的顺序分开发送给各相应电路单元处理的技术.比方Core乱序执行引擎说程序某一段有7 条指令,此时CPU ...
- TCP 协议如何保证可靠传输
一.综述 1.确认和重传:接收方收到报文就会确认,发送方发送一段时间后没有收到确认就重传. 2.数据校验 3.数据合理分片和排序: UDP:IP数据报大于1500字节,大于MTU.这个时候发送方IP层 ...
- background image position问题
在CSS中,背景图片的定位方法有3种: 1)关键字:background-position: top left; 2)像素:background-position: 0px 0px; 3)百分比:ba ...
- 个人所得税计算器2016 by Jacksile
个人所得税计算器2016 // (83500+i)) { var to=(all*45/100-13505).toFixed(2); document.getElementById("int ...
- CSS学习笔记——定位position属性的学习
今天学习之前剩下的一个问题:CSS的position属性.首先归纳出和position相关的问题: position作为一个属性,它一共有哪几个属性值? position常用的属性值有哪几个?分别有什 ...