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.

  1. Python 2.7.6 (default, Jun 22 2015, 18:00:18)
  2. [GCC 4.8.2] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> g = (x * 2 for x in xrange(2, 27))
  5. >>> g
  6. <generator object <genexpr> at 0xb71aff04>
  7. >>> g.next()
  8. 4
  9. >>> g.next()
  10. 6
  11. >>> l = [x * 2 for x in xrange(2, 27)]
  12. >>> l
  13. [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]
  14. >>> type(l)
  15. <type 'list'>
  16. >>> type(g)
  17. <type 'generator'>
  18. >>> l.next()
  19. Traceback (most recent call last):
  20. File "<stdin>", line 1, in <module>
  21. AttributeError: 'list' object has no attribute 'next'
  22. >>> g[0]
  23. Traceback (most recent call last):
  24. File "<stdin>", line 1, in <module>
  25. TypeError: 'generator' object has no attribute '__getitem__'
  26. >>>

生成器推导式( ), 列表推导式[ ]

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. Hadoop 安装指南

    一.安装JDK 1.用户可以在Oracle JDK的官网下载相应版本的JDK,本例以JDK 1.6为例,官网地址为http://www.oracle.com/tech-network/java/jav ...

  2. OkHttp+Stetho+Chrome调试android网络部分(原创)

    android网络调试一直是一个比较麻烦的部分,因为在不同序列的请求中,返回的数据会有不同的变化,如果能像web开发一样使用调试功能查看页面的访问数据该是多么美好的事情! 很幸运的是,现在Androi ...

  3. Enable Authentication on MongoDB

    1.Connect to the server using the mongo shell mongo mongodb://localhost:27017 2.Create the user admi ...

  4. 使用Fiddler调试线上JS代码

    在下面的命令框输入“select script”回车来筛选js请求 将HTTP请求重定向到本地的文件,进行web调试.这种调试方式不需要发布到线上再验证,避免了修改不成功.对用户造成影响的风险 左边一 ...

  5. Jmeter接口测试系列之参数化方法

    至于参数化的用途,我这里就不多说了,本文主要介绍最全.最强大的参数化方法,对参数化有一个彻底的认识,这里提供了多种参数化方法 1.jmeter参数化之用户变量   在测试计划里面添加一个用户自定义的变 ...

  6. 第一百五十一节,封装库--JavaScript,表单验证--密码确认验证--回答验证--电子邮件验证加自动补全

    封装库--JavaScript,表单验证--密码确认验证--回答验证--电子邮件验证加自动补全 效果图 html <div id="reg"> <h2 class ...

  7. 【BZOJ】3391: [Usaco2004 Dec]Tree Cutting网络破坏(dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3391 显然判断每个点只需要判断子树是否小于等于n/2即可 那么我们虚拟一个根,然后计算每个子树的si ...

  8. 【Raspberry pi+python+mysql】红外传感器-发邮件-存数据库

    1.mysql http://dev.mysql.com/doc/refman/5.5/en/tutorial.html mysql+python http://dev.mysql.com/doc/c ...

  9. SurvivalShooter学习笔记(一.相机跟随)

    1.场景碰撞已好,地板需建一Quad去掉渲染留下碰撞,设置layer为Floor:用于建立摄像机朝向地面的射线,确定鼠标停留点,确定主角需要的朝向. 2.设置摄像机跟随主角: 本例中摄像机设置为正交模 ...

  10. JS制作一个通用的商城版历史浏览记录

    正在开发一个b2c的国外商城,昨天做了一个历史浏览记录发出来跟大家分享一下. JS: //cookie相关函数 function getCookieVal(offset) {    var endst ...