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的更多相关文章

  1. Python 列表解析list comprehension和生成表达式generator expression

    如果想通过操作和处理一个序列(或其他的可迭代对象)来创建一个新的列表时可以使用列表解析(List comprehensions)和生成表达式(generator expression) (1)list ...

  2. Python 3. 里filter与generator expression的区别

    # -*- coding: utf-8 -*- """ A test to show the difference between filter and genrator ...

  3. 详解Python中的生成器表达式(generator expression)

    介绍 1.生成器表达式(generator expression)也叫生成器推导式或生成器解析式,用法与列表推导式非常相似,在形式上生成器推导式使用圆括号(parentheses)作为定界符,而不是列 ...

  4. django1.11 启动错误:Generator expression must be parenthesized

    错误信息: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0 ...

  5. SyntaxError Generator expression must be parenthesized

    环境: Windows10 python3.7.0 Django1.11.15 异常 启动Django时抛出以下异常: Unhandled exception in thread started by ...

  6. python3.7环境下创建app、运行Django1.11版本项目报错Generator expression must be parenthesized

    有些同学喜欢追求新鲜感~但追求新鲜感终归是要付出一点点代价的 在编程领域有一句至理名言:用东西不要用最新的! 就像每次苹果系统的升级都会有相当一部分用户的手机成砖一样 下面我们就介绍一个因版本升级带来 ...

  7. django 启动错误:Generator expression must be parenthesized 错误信息:

    错误为: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x ...

  8. python3.7环境下创建app,运行Django1.11版本项目报错SyntaxError: Generator expression must be parenthesized

    咳咳!!! 今天用命令行创建django项目中的app应用,出现了这样一个错误 这个错误在python3.6版本下安装运行django 1.11版本正常运行,但python3.7版本下运行django ...

  9. 启动Django报错:SyntaxError: Generator expression must be parenthesized 解决办法

    这是因为版本不兼容所导致的. 此错误已知与Python问题#32012相关.基于Django 1.11.16及以下的项目将在Python 3.7启动时引发此异常.此问题的补丁已合并到Django 2. ...

随机推荐

  1. 有效提升大数据量写入excel的效率

    在开发过程中经常会有需要将数据导出到 excel 的需求,当数据量很大,达到几万甚至几十万.几百万级别的时候,如何加快生成 excel 的速度呢?首先普及一下知识背景:Excel2003 及以下版本一 ...

  2. J2EE之Servlet初见

    Servlet是J2EE12种规范之中的一个.它也是用java语言编写的程序,其本身也是一种JAVA类,在须要的时候被实例化,不须要的时候自己主动销毁,Servlet的执行是在Servlet容器内执行 ...

  3. STM32F10x_RTC日历

    Ⅰ.概述 接着上一篇文章来讲述关于RTC的计数功能,我们以实例RTC日历(读写年.月.日.星期.时.分.秒)来讲述该章节. STM32F1系列芯片的RTC功能和其他系列(F0.F2.F4等)相比来说, ...

  4. java模拟http请求上传文件,基于Apache的httpclient

    1.依赖 模拟http端的请求需要依赖Apache的httpclient,需要第三方JSON支持,项目中添加 <dependency> <groupId>org.apache& ...

  5. 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderList

    最近在用idea部署war文件的时候,总是出现了部署失败的错误,刚开始并没有在意,但是现在次数越来越多了,不得不在意了,然后就在百度上搜,然后就有了各种说法 1,错误的信息是: One or more ...

  6. python web框架 推荐

    Flask 很轻,花很少的成本就能够开发一个简单的网站.非常适合初学者学习. 学会以后,可以考虑学习插件的使用,用 SQLAlchemy + Flask-SQLAlchemy 来对你的数据库进行控制. ...

  7. Mathematica之基本操作

    1.清楚所有变量 Clear["Global`*"];

  8. Enterprise Architect UML建模

    UML建模 前言 UML建模资料已经很多了,有人想用有人不用,有人会用也有人不会用,本文只是作者的一篇UML建模总结,不想去写太细,因为真正的你去用下,去画下就基本都会了.工具毕竟是工具,设计和思想才 ...

  9. Chrome浏览器快捷键(收藏!)

  10. hdu 2485(最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2485 思路:题目的意思是删除最少的点使1,n的最短路大于k.将点转化为边,容量为1,费用为0,然后就是 ...