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. ...
随机推荐
- mui调用webservice
document.getElementById("confirm").addEventListener('tap', function() {var respnoseEl = do ...
- 怎样用Google APIs和Google的应用系统进行集成(5)----怎样把Google Tasks的JSON Schema转换成XML的Schema(XSD)?
前面说了一些Google API的介绍,可是在实际的开发其中,我们可能须要把Google RESTful API返回的JSON数据转换成XML数据输入到第三方系统,这在企业应用集成里面很的常见. 那么 ...
- jQuery 实战读书笔记之第二章:选择元素
基本选择器 html 代码如下,后面的 js 使用的 html 基本大同小异. <!doctype html> <html> <head> <title> ...
- UVALive 7721 K - 2-ME Set 集合dp,所有数的位或来表示状态。
/** 题目:UVALive 7721 K - 2-ME Set 链接:https://vjudge.net/problem/UVALive-7721 题意:给定n个数,从中取出一个集合,至少包含两个 ...
- 给jdk配置jvm的参数
(1)window->preference->java->installed JREs ->edit -Xms512m -Xmx512m -XX:MaxNewSize=512 ...
- 20140710文安c++面试总结
这次去文安面试并未是我想象中的那个样子,可能有如下原因: 1.招聘旺季已过,仅剩下c++这个职位 2.并未做过面试前大公司的面试技巧-做面试题 面试过程基本就是先做面试题: 1.试题分布式-逻辑题.分 ...
- crontab读不来环境变量
无法识别java nohup: failed to run command `java': No such file or directory 那么在shell中加两行即可解决 . /etc/prof ...
- python技巧之下划线(一)
1.python的moudles文件中__all__作用 Python的moudle是很重要的一个概念,我看到好多人写的moudle里都有一个__init__.py文件.有的__init__.py中是 ...
- Android UI开发第三十六篇——使用Volley加载图片列表
Android开发者可能会使用Universal Image Loader或者Square`s newer Picasso这些第三方的库去处理图片的加载,那么Volley是怎么加载图片列表的呢,这一篇 ...
- fis3 部署手册
为什么使用FIS3 项目上线一段时间后如果更新JS或CSS文件,而客户端已经对该文件缓存过了,那就有可能会无法及时更新而继续采用旧的JS或CSS文件,无法达到想要的效果. 处理类似情况最有效的解决方案 ...