python iterators and generators of

2010-02-02  来源:本站原创  分类:Python  人气:208 

Both are to generate a cache list, the generator (generators) is more like an enhanced iterators (iterators). Both are using the next () to obtain the data;

Iterator: (use iter () method)

a = 'abc';

it = iter (a); print it.next ()

For iterator is a classic use.

for i in a: print i (for that is when the error occurred stopIteration that exit the loop)

Builder: (use the yield keyword)

Builder to launch the data using the keyword yield; generator also has send (val) to modify the value and close () to close the generator;

Example: Feibolaqie series:

def fib():

    a,b = 0,1

    while True:

        yield b

        a,b = b,a+b

 f = fib()

 print [f.next() for i in range(100)]
相关文章
  • python iterators and generators of 2010-02-02

    Both are to generate a cache list, the generator (generators) is more like an enhanced iterators (iterators). Both are using the next () to obtain the data; Iterator: (use iter () method) a = 'abc'; it = iter (a); print it.next () For iterator is a c

  • Learning Python: iterators 2010-11-29

    1, What is an iterator iterator is to provide next () method of the object 2, how to use iterators for v in it: python's iterator for statements can be processed, it automatically calls the iterator's next () method to iterate, and terminate in the e

  • Speaking from the python's yield 2010-02-03

    Some time ago in the time trac in the wiki module when the source and found a lot of places use yiled the words Feeling is in place need to return a value instead of by yield return, Not quite understand its usage, so detailed studies. One uses the y

  • useful sites for learning python 2010-11-26

    Python Language Study == Overview == . For a programming language, syntax is the most intuitive performance, a variety of class library API is the most direct application. . But under the surface, the infrastructure of language, models, principles an

  • 详解Python的迭代器.生成器以及相关的itertools包 2014-03-01

    这篇文章主要介绍了详解Python的迭代器.生成器以及相关的itertools包,Iterators.Generators是Python的高级特性,亦是Python学习当中必会的基本知识,需要的朋友可以参考下 对数学家来说,Python这门语言有着很多吸引他们的地方.举几个例子:对于tuple.lists以及sets等容器的支持,使用与传统数学类似的符号标记方式,还有列表推导式这样与数学中集合推导式和集的结构式(set-builder notation)很相似的语法结构. 另外一些很吸引数学爱好

  • Python高级特性(3): Classes和Metaclasses 2014-11-03

    Python高级特性(1):Iterators.Generators和itertools Python高级特性(2):Closures.Decorators和functools 类和对象 类和函数一样都是Python中的对象.当一个类定义完成之后,Python将创建一个"类对象"并将其赋值给一个同名变量.类是type类型的对象(是不是有点拗口?). 类对象是可调用的(callable,实现了 __call__方法),并且调用它能够创建类的对象.你可以将类当做其他对象那么处理.例如,你能

  • Python中如何使用yield,对于庞大迭代的优化处理 2013-08-02

    一直困扰于yield的使用,今天看到一篇不错的解释,虽然都是英文,不过不要紧,可以跳开,直接看代码的部分就能懂 Improve Your Python: 'yield' and Generators Explained Posted on Apr 07, 2013 by Jeff Knupp Prior to beginning tutoring sessions, I ask new students to fill out a brief self-assessment where they

  • Python client for Redis 官翻文档2.10.1(五) 2014-06-21

    LUA Scripting LUA脚本 redis-py supports the EVAL, EVALSHA, and SCRIPT commands. However, there are a number of edge cases that make these commands tedious to use in real world scenarios. Therefore, redis-py exposes a Script object that makes scripting

  • python greenlet 2015-04-26

    greenlet: Lightweight concurrent programming Motivation The "greenlet" package is a spin-off of Stackless, a version of CPython that supports micro-threads called "tasklets". Tasklets run pseudo-concurrently (typically in a single or a

  • Small is Beautiful: the design of Lua finishing speech translation 2010-10-18

    Small is Beautiful: the design of Lua finishing speech translation The speech was very well written, summarizes a lot of noteworthy things Lua, Lua is a good entry-learning materials, so I have translated and recorded. 10 Mar 2010 Roberto talks at th

  • C++.Java.JavaScript中迭代器的用法 2015-01-10

    编程思想之迭代器 什么是迭代器? 迭代器(Iterator)是按照一定的顺序对一个或多个容器中的元素从前往遍历的一种机制,比如for循环就是一种最简单的迭代器,对一个数组的遍历也是一种的迭代遍历的过程.GOF给出的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部细节.迭代器有时也称为枚举器(Enumerator),其结构图如下: 迭代器结构图 迭代器其实就是维护一个当前的指针,这个指针可以指向当前的元素,可以返回当前所指向的元素,可以移到下一个元素的

  • Python高级特性(1):Iterators.Generators和itertools 2015-02-04

    [译注]:作为一门动态脚本语言,Python对编程初学者而言很友好,丰富的第三方库能够给使用者带来很大的便利.而Python同时也能够提供一些高级的特性方便用户使用更为复杂的数据结构.本系列文章共有三篇,本文是系列的第一篇,将会介绍迭代器.生成器以及itertools模块的相关用法.由于作者 Sahand Saba 列举的示例中有诸多专业的数学相关内容,因此翻译中有诸多不妥之处请大家指出,非常感谢. 对数学家来说,Python这门语言有着很多吸引他们的地方.举几个例子:对于tuple.lists

  • Python解释yield和Generators(生成器) 2014-05-27

    见链接:

  • Python 2.5 Quick Reference 2010-06-21

    Python 2.5 Quick Reference Collection <script type="text/javascript"> document.body.oncopy = function () (if (window.clipboardData) (setTimeout (function () (var text = clipboardData.getData ("text"); if (text & ; & text.

  • 详细介绍Python函数中的默认参数 2014-01-22

    这篇文章主要介绍了详细介绍Python函数中的默认参数,包括默认参数的传递和求值等内容,需要的朋友可以参考下 import datetime as dt def log_time(message, time=None): if time is None: time=dt.datetime.now() print("{0}: {1}".format(time.isoformat(), message)) 最近我在一段Python代码中发现了一个因为错误的使用默认参数而产生的非常恶心的bu

  • Python常用模块介绍 2014-06-24

    这篇文章主要介绍了Python常用模块介绍,本文罗列了如python运行时服务.数学.数据结构.算法和代码简化.string 和 text 处理.python数据库访问等模块,需要的朋友可以参考下 python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的libraries(modules)如下: 1)python运行时服务 * copy: copy模块提供了对复合(compound)对象(list,

  • Python的迭代器和生成器使用实例 2014-12-23

    这篇文章主要介绍了Python的迭代器和生成器使用实例,本文讲解了迭代器的next方法.__iter__方法和实例以及生成器的代码实例,需要的朋友可以参考下 一.迭代器Iterators 迭代器仅是一容器对象,它实现了迭代器协议.它有两个基本方法: 1)next方法 返回容器的下一个元素 2)__iter__方法 返回迭代器自身 迭代器可使用内建的iter方法创建,见例子: >>> i = iter('abc') >>> i.next() 'a' >>>

  • python简介(维基) 2013-04-03

    Python 维基百科,自由的百科全书 跳转至: 导航, 搜索 本文介绍的是 计算机程序设计语言.关于蟒蛇,详见" 蟒蛇". Python 编程范型 多泛型.面向对象.函数式编程.面向切面编程 发行时间 1991年 设计者 吉多·范罗苏姆(Guido van Rossum) 实现者 Python Software Foundation 最新发行时间 3.3.0/ 2012年10月1日:5个月前 (2012-10-01) 3.2.3/ 2012年4月11日:11个月前 (2012-04-

  • [原创]Python 之快速性能优化(第一部分) 2013-11-19

    本文为翻译, 原文地址:< Quick Python Performance Optimization: Part I > This is part I of a two part series of blog post on performance optimization in python. Aim is to just explain, the right way to do simple things which we use in day-to-day python program

  • Python Essentials: 2 (classic tutorial new revision, 10 projects fascinating) 2010-06-28

    Python Essentials: 2 (classic tutorial new revision, 10 projects fascinating) Basic Information Original title: Beginning Python: From Novice to Professional, Second Edition Former Press: Apress Author: (Norway) Magnus Lie Hetland Translator: Secreta