Python的迭代器和生成器使用实例

yipeiwu_com6年前Python基础

一、迭代器Iterators

迭代器仅是一容器对象,它实现了迭代器协议。它有两个基本方法:

1)next方法
返回容器的下一个元素

2)__iter__方法
返回迭代器自身

迭代器可使用内建的iter方法创建,见例子:

复制代码 代码如下:

>>> i = iter('abc')
>>> i.next()
'a'
>>> i.next()
'b'
>>> i.next()
'c'
>>> i.next()
Traceback (most recent call last):
  File "<string>", line 1, in <string>
StopIteration:

class MyIterator(object):
  def __init__(self, step):
  self.step = step
  def next(self):
  """Returns the next element."""
  if self.step==0:
  raise StopIteration
  self.step-=1
  return self.step
  def __iter__(self):
  """Returns the iterator itself."""
  return self
for el in MyIterator(4):
  print el
--------------------


结果:
复制代码 代码如下:

3
2
1
0

二、生成器Generators

从Python2.2起,生成器提供了一种简洁的方式帮助返回列表元素的函数来完成简单和有效的代码。
它基于yield指令,允许停止函数并立即返回结果。

此函数保存其执行上下文,如果需要,可立即继续执行。

例如Fibonacci函数:

复制代码 代码如下:

def fibonacci():
  a,b=0,1
  while True:
  yield b
  a,b = b, a+b
fib=fibonacci()
print fib.next()
print fib.next()
print fib.next()
print [fib.next() for i in range(10)]
--------------------

结果:
复制代码 代码如下:

1
1
2
[3, 5, 8, 13, 21, 34, 55, 89, 144, 233]

PEP Python Enhancement Proposal Python增强建议

tokenize模块

复制代码 代码如下:

>>> import tokenize
>>> reader = open('c:/temp/py1.py').next
>>> tokens=tokenize.generate_tokens(reader)
>>> tokens.next()
(1, 'class', (1, 0), (1, 5), 'class MyIterator(object):/n')
>>> tokens.next()
(1, 'MyIterator', (1, 6), (1, 16), 'class MyIterator(object):/n')
>>> tokens.next()
(51, '(', (1, 16), (1, 17), 'class MyIterator(object):/n')

例子:
复制代码 代码如下:

def power(values):
  for value in values:
  print 'powering %s' %value
  yield value
def adder(values):
  for value in values:
  print 'adding to %s' %value
  if value%2==0:
  yield value+3
  else:
  yield value+2
elements = [1,4,7,9,12,19]
res = adder(power(elements))
print res.next()
print res.next()
--------------------

结果:
复制代码 代码如下:

powering 1
adding to 1
3
powering 4
adding to 4
7

保持代码简单,而不是数据。
注意:宁可有大量简单的可迭代函数,也不要一个复杂的一次只计算出一个值的函数。

例子:

复制代码 代码如下:

def psychologist():
  print 'Please tell me your problems'
  while True:
  answer = (yield)
  if answer is not None:
  if answer.endswith('?'):
  print ("Don't ask yourself too much questions")
  elif 'good' in answer:
  print "A that's good, go on"
  elif 'bad' in answer:
  print "Don't be so negative"
free = psychologist()
print free.next()
print free.send('I feel bad')
print free.send("Why I shouldn't ?")
print free.send("ok then i should find what is good for me")
--------------------

结果:
复制代码 代码如下:

Please tell me your problems
None
Don't be so negative
None
Don't ask yourself too much questions
None
A that's good, go on
None

相关文章

python3.6下Numpy库下载与安装图文教程

python3.6下Numpy库下载与安装图文教程

今天在做Plotly的散点图时,需要Numpy 这个库的使用。 没有安装Numpy这个库的时候,报错一般是下图这样:ModuleNotFoundError: No module name...

Python3.5 Pandas模块缺失值处理和层次索引实例详解

Python3.5 Pandas模块缺失值处理和层次索引实例详解

本文实例讲述了Python3.5 Pandas模块缺失值处理和层次索引。分享给大家供大家参考,具体如下: 1、pandas缺失值处理 import numpy as...

python utc datetime转换为时间戳的方法

最近python代码遇到了一个神奇的需求, 就是如果将python utc datetime转换为时间戳. 百度找到都是使用time.mktime(xxx) 但是看到官网文档里写 t...

跟老齐学Python之不要红头文件(2)

文件的属性 所谓属性,就是能够通过一个文件对象得到的东西。 复制代码 代码如下: >>> f = open("131.txt","a") >>> f....

Python读写Excel文件的实例

Python读写Excel文件的实例

最近由于经常要用到Excel,需要根据Excel表格中的内容对一些apk进行处理,手动处理很麻烦,于是决定写脚本来处理。首先贴出网上找来的读写Excel的脚本。 1.读取Excel(需要...