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. ...
随机推荐
- 分析并实现 360 P1路由器上的朋友专享网络 功能
笔者分析了360 P1路由器上的朋友专享网络功能,发现其主要由如下子功能组成: 1. APP点击“立即开启”,则路由器会多出一个新的SSID:360朋友专享网络-8463.此SSID不加密:同时,原有 ...
- 微信小程序-page
一 什么是page() page(),是一个函数,用来注册一个页面, 接受一个object参数, 指定页面的初始数据,生命周期函数,事件处理函数 等等 object参数说明: (1)data (obj ...
- sublime3 SublimeREPL python3
https://blog.csdn.net/lylfv/article/details/81453016
- Windows的静态库使用步骤
windows库程序: 1.静态库程序 - 运行时不独立存在,会被链接到可执行文件或者动态库中,目标程序的归档. 文件扩展名:LIB 2.动态库程序 - 运行时独立存在,不会被链接到可执行文件或其他动 ...
- java程序调用kettle
(1).将相应的kettle的jar包导入的java项目,主要的jar包有一下几个. (2).java程序. package cn.com.taiji.oosweb.test.web; import ...
- springboot查找配置文件路径的过程
spring加载配置文件是通过listener监视器实现的,在springboot启动时: 在容器启动完成后会广播一个SpringApplicationEvent事件,而SpringApplicati ...
- NPOI 1.2教程(目录)操作Excel
原文地址:http://www.cnblogs.com/atao/archive/2009/11/15/1603528.html
- SRM 719 Div 1 250 500
250: 题目大意: 在一个N行无限大的网格图里,每经过一个格子都要付出一定的代价.同一行的每个格子代价相同. 给出起点和终点,求从起点到终点的付出的最少代价. 思路: 最优方案肯定是从起点沿竖直方向 ...
- datagrid使用要点
table自适应: (fit:true(设置table)) 列自动撑开:fitColumns: true,注意给列的width属性赋值
- Java 反射机制[Field反射]
Java 反射机制[Field反射] 1. 反射概念及功能 反射就是把Java类中的各种成分映射成对应的Java类.比如一个Java类中用一个Class类的对象来表示. 一个类中的组成部分分为成员变 ...