4.迭代器和生成器
4.1手动访问迭代器的元素
'''
可迭代的对象(iterable):实现了__iter__()方法,不可通过next()获取元素,且元素个数清楚
迭代器(itertor):实现了__iter__()和__next__()方法,可通过next()获取元素并且是接着上一次的next()往下,也可通过for循环获取元素
'''
myList1 = iter([1, 2, 3, 4, 5, 6, 7, 8, 9])
myList2 = iter([10, 11, 12, 13, 14, 15, 16, 17])
try:
while True:
print(next(myList1))
except StopIteration:
print("迭代停止")
#从None开始
while True:
temp = next(myList1, None)
if temp is None:
break
print(temp)
#设置结束返回值
while True:
temp = next(myList2, None)
if temp is None:
break
print(temp)
4.2委托迭代
'''
构建了一个自定义的容器对象,内部持有列表、元组或其他可迭代的对象。
想让容器自身能够完成迭代操作
'''
class Node(object):
def __init__(self, value):
self._value = value
self._children = []
def __repr__(self):
return 'Node({!r})'.format(self._value)
def add_children(self, node):
self._children.append(node)
def __iter__(self):
#将迭代任务转发给children
return iter(self._children)
if __name__ == "__main__":
root = Node(0)
child1 = Node(1)
child2 = Node(2)
root.add_children(child1)
root.add_children(child2)
for ch in root:
print(ch)
4.3用生成器创建新的迭代模式
'''
生成器是一种特殊的迭代器,响应迭代操作时执行
通过yield语句和生成器表达式实现, 一次只有一个元素占用内存,所以生成器占用内存小
'''
def frange(start, stop, increment):
x = start
while x < stop:
yield x
x = x+increment
for n in frange(0, 4, 0.5):
print(n)
#生成器函数的执行,从迭代操作开始到yield语句,再从yield语句开始直到迭代停止
def countdown(n):
print("Starting to count from", n)
while n > 0:
yield n
n -= 1
print("Done!")
c = countdown(3)
print(c)
print(next(c))
print(next(c))
print(next(c))
print(next(c))
4.4实现迭代协议
'''
构建一个自定义对象,可以支持迭代操作,以简单的方式实现迭代协议
'''
#深度优先遍历树的节点
class Node(object):
def __init__(self, value):
self._value = value
self._children = []
def __repr__(self):
return 'Node({!r})'.format(self._value)
def add_child(self, child):
self._children.append(child)
def __iter__(self):
return iter(self._children)
def depth_first(self):
yield self
for c in self:
#调用字节的depth_first()返回子节点的子节点
#yield from generator 实际上是返回另外一个生成器
#yield from iterable 本质上等于for item in iterable: yield item
yield from c.depth_first()
if __name__ == "__main__":
root = Node(0)
child1 = Node(1)
child2 = Node(2)
root.add_child(child1)
root.add_child(child2)
child1.add_child(Node(3))
child1.add_child(Node(4))
child2.add_child(Node(5))
for ch in root.depth_first():
print(ch)
#另一种迭代实现
class AnotherNode(Node):
def __init__(self, value):
super(AnotherNode, self).__init__(value)
def depth_first(self):
return DepthFirstIterator(self)
class DepthFirstIterator(object):
def __init__(self, start_node):
self._node = start_node
self._children_iter = None
self._child_iter = None
def __iter__(self):
return self
def __next__(self):
if self._child_iter is None:
self._child_iter = iter(self._node)
return self._node
elif self._child_iter:
try:
nextchild = next(self._child_iter)
return nextchild
except StopIteration:
self._child_iter = None
return next(self)
else:
self._child_iter = next(self._child_iter).depth_first()
return next(self)
4.5反向迭代
a = [1, 2, 3, 4]
#反向迭代只有在待处理的对象拥有可确定的大小或者对象实现了__reversed__()特殊方法
#不满足这两个条件必须把对象转化为列表
for x in reversed(a):
print(x)
class Countdown:
def __init__(self, start):
self.start = start
def __iter__(self):
n = self.start
while n > 0:
yield n
n -= 1
def __reversed__(self):
n = 1
while n <= self.start:
yield n
n += 1
4.6定义带有额外状态的生成器函数
'''
定义一个生成器函数,但它涉及一些额外状态,希望能以某种形式
将这些状态暴露给用户
'''
from collections import deque
class linehistory(object):
def __init__(self, lines, histlen=3):
self.lines = lines
self.history = deque(maxlen=histlen)
def __iter__(self):
#enumerate()返回下标和可迭代对象元素的元组序列,下标起始数默认0
for lineno, line in enumerate(self.lines, 1):
self.history.append((lineno, line))
yield line
def clear(self):
self.history.clear()
with open('somefile.txt') as f:
lines = linehistory(f)
for line in lines:
if 'python' in line:
for lineno, hline in lines.history:
print('{}: {}'.format(lineno, hline), end=' ')
4.7对迭代器做切片处理
'''
想对由迭代器产生的数据做切片处理,但普通切片操作符不管用
'''
def count(n):
while True:
yield n
n -= 1
c = count(0)
import itertools
#islice()产生一个迭代器,它通过访问并丢弃所有起始索引之前的元素来实现的
#之后的元素由islice对象产生,直到索引结束
#由于迭代器中的元素只能访问一次,islice会消耗掉所提供的迭代器中的数据
for x in itertools.islice(c, 10, 20):
print(x)
4.8跳过可迭代对象中的前一部分元素
import itertools
items = [0, 2, 4, 3, 5, 6, 9, 8]
#dropwhile()提供一个函数和一个可迭代对象, 函数作为筛选前面符合函数的数并丢弃
for i in itertools.dropwhile(lambda s: s % 2==0, items):
print(i)
#知道要跳过多少个元素
items = ['a', 'b', 'c', 1, 4, 10, 15]
#None表示前三个元素之外的所有元素
for i in itertools.islice(items, 3, None):
print(i)
本文深入探讨了Python中迭代器和生成器的概念及应用,包括手动访问迭代器元素、委托迭代、生成器创建、迭代协议实现、反向迭代等核心主题,以及如何处理迭代器切片和跳过前部元素的高级技巧。
294

被折叠的 条评论
为什么被折叠?



