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. ...
随机推荐
- The Definitive Guide To Django 2 学习笔记(九) 第五章 模型 (一)数据库访问
以MySql数据库为例,先到http://dev.mysql.com/downloads/connector/python/处下载MysqlConnector for python的连接器. from ...
- Chapter 2 JavaScript Basic
Has 5 primitive types: Undefined, Null, Boolean, String, Number. typeof operator Undefined return u ...
- 设置Tomcat编码(UTF-8)
Tomcat的默认编码是ISO-8859-1,如果有是get请求时,会出现乱码,这种情况可以修改Tomcat的编码解决. 在tomcat的conf目录下,编辑server.xml配置文件,在Conne ...
- php中while($row = $results->fetch_row())调用出错
php中while($row = $results->fetch_row())调用出错 错误处在sql语句上
- Linux 设备驱动的固件载入
作为一个驱动作者, 你可能发现你面对一个设备必须在它能支持工作前下载固件到它里面. 硬件市场的很多地方的竞争是如此得强烈, 以至于甚至一点用作设备控制固件的 EEPROM 的成本制造商都不愿意花费. ...
- Character流与Byte流的区别
Character流与Byte流的区别 (2012-09-25 16:15:49) 标签: 杂谈 分类: 大赛指导 Character流与Byte流的区别是 A) 每次读入的字节数不同 B) 前者带有 ...
- flask 邮箱配置
http://blog.csdn.net/stan_pcf/article/details/51098126 先进入邮箱设置 POP3/SMTP/IMAP 下面代码来自知乎 https://www.z ...
- python 糗事百科实例
爬取糗事百科段子,假设页面的URL是 http://www.qiushibaike.com/8hr/page/1 要求: 使用requests获取页面信息,用XPath / re 做数据提取 获取每个 ...
- XMLHttpRequest的withCredentials属性
最近对接第三方网站出现一下错误:Access to XMLHttpRequest at 'https://third.site.com/request_url' from origin 'https: ...
- FireBug与FirePHP
a.FireBug,平时用的比较多.就是在客户端调试代码.如:hTML ,CSS,JS等 b.FireBug安装较容易. b-1,打开火狐浏览器 b-2,打开“附加组件” b-3.搜索“firebug ...