list comprehension & generator expression
List comprehensions(列表推导式) are better when you want to iterate over something multiple times. However,
it's also worth noting that you should use a list if you want to use any of the list methods. Basically, use a
generator expression(生成器推导式/生成器表达式) if all you're doing is iterating once. If you want to store and
use the generated results, then you're probably better off with a list comprehension.
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> g = (x * 2 for x in xrange(2, 27))
>>> g
<generator object <genexpr> at 0xb71aff04>
>>> g.next()
4
>>> g.next()
6
>>> l = [x * 2 for x in xrange(2, 27)]
>>> l
[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52]
>>> type(l)
<type 'list'>
>>> type(g)
<type 'generator'>
>>> l.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'next'
>>> g[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'generator' object has no attribute '__getitem__'
>>>
生成器推导式( ), 列表推导式[ ]
Reference:
Generator Expressions vs. List Comprehension: http://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehension
list comprehension & generator expression的更多相关文章
- Python 列表解析list comprehension和生成表达式generator expression
如果想通过操作和处理一个序列(或其他的可迭代对象)来创建一个新的列表时可以使用列表解析(List comprehensions)和生成表达式(generator expression) (1)list ...
- Python 3. 里filter与generator expression的区别
# -*- coding: utf-8 -*- """ A test to show the difference between filter and genrator ...
- 详解Python中的生成器表达式(generator expression)
介绍 1.生成器表达式(generator expression)也叫生成器推导式或生成器解析式,用法与列表推导式非常相似,在形式上生成器推导式使用圆括号(parentheses)作为定界符,而不是列 ...
- django1.11 启动错误:Generator expression must be parenthesized
错误信息: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0 ...
- SyntaxError Generator expression must be parenthesized
环境: Windows10 python3.7.0 Django1.11.15 异常 启动Django时抛出以下异常: Unhandled exception in thread started by ...
- python3.7环境下创建app、运行Django1.11版本项目报错Generator expression must be parenthesized
有些同学喜欢追求新鲜感~但追求新鲜感终归是要付出一点点代价的 在编程领域有一句至理名言:用东西不要用最新的! 就像每次苹果系统的升级都会有相当一部分用户的手机成砖一样 下面我们就介绍一个因版本升级带来 ...
- django 启动错误:Generator expression must be parenthesized 错误信息:
错误为: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x ...
- python3.7环境下创建app,运行Django1.11版本项目报错SyntaxError: Generator expression must be parenthesized
咳咳!!! 今天用命令行创建django项目中的app应用,出现了这样一个错误 这个错误在python3.6版本下安装运行django 1.11版本正常运行,但python3.7版本下运行django ...
- 启动Django报错:SyntaxError: Generator expression must be parenthesized 解决办法
这是因为版本不兼容所导致的. 此错误已知与Python问题#32012相关.基于Django 1.11.16及以下的项目将在Python 3.7启动时引发此异常.此问题的补丁已合并到Django 2. ...
随机推荐
- J2EE之Servlet初见
Servlet是J2EE12种规范之中的一个.它也是用java语言编写的程序,其本身也是一种JAVA类,在须要的时候被实例化,不须要的时候自己主动销毁,Servlet的执行是在Servlet容器内执行 ...
- [转]所有编程皆为 Web 编程
Web编程还远远没有达到完美的境地.其实,还有点乱!没错,随便会写点代码的人就能三下两下地搞出一个糟糕的Web应用:也确实,99%的Web 应用都似狗屎一堆.但是,这也意味着,相当“聪明”的程序员们正 ...
- IOS证书之Certificates,Devices, Identifiers & Profiles
做IOS开发的,在需要发布应用的时候,会接触到iOS Dev Center里面的证书制作,按照网上的资料操作,我们可以很容易的制作证书并且完成真机调试或者是产品发布,但是对于Certificates. ...
- Handler vs Timer,究竟该用哪个?
Handler vs Timer 在我们Android开发过程中,经常需要执行一些短周期的定时任务,这时候有两个选择Timer或者Handler.然而个人认为:Handler在多个方面比Timer更为 ...
- Python 使用标准库根据进程名获取进程PID
应用场景 在进行 Linux 运维的环境中,我们经常会遇到维护同一台服务器上的多个程序,涉及到程序的启动.关闭和重启操作. 通常这些程序之间存在着相互依存的关系需要进行依次的启动关闭操作. 下面介绍几 ...
- 在Intellij IDEA下用X-debug调试PHP
用Intellij IDEA使用X-debug来调试PHP,主要需要配置的部分有三个地方,分别为php.ini的配置,IDEA的配置和浏览器的配置,主要如下: php.ini(wamp修改的是phpF ...
- 蓝桥杯 C/C++参考题目 开平方(数学题,迭代法求开方)
开平方 如果没有计算器,我们如何求2的平方根?可以先猜测一个数,比如1.5,然后用2除以这个数字.如果我们猜对了,则除法的结果必然与我们猜测的数字相同.我们猜测的越准确,除法的结果与猜测的数字就越接近 ...
- flask配置加载几种方式
方法一.直接配置 app.config['HOST']='xxx.a.com' print(app.config.get('HOST')) 方法二.通过环境变量加载配置 环境变量:export MyA ...
- Laravel的学习网站推荐
Laravel官网 网址是:https://www.laravel.com,里面有优质的教程和文档 只不过是英文的. Laravel学院 网址是:http://laravelacademy.org/, ...
- ilbc编解码在android实现
iLBC 是为专为提供稳健的 IP 语音通信而开发的语音 codec,以窄带语音为设计基础,具有 8 kHz 的采样率.iLBC codec 支持两种基本的帧长度:13.3 kbps 比特率下编码帧长 ...