Python–cookbook–4.迭代器与生成器

Python–cookbook–4.迭代器与生成器

导入对应模块

# -*- coding: UTF-8 -*-  # 保证编码格式是utf8
from collections import deque
import itertools
import heapq

手动遍历迭代器

def manual_iter():
    with open('test.txt', encoding='utf8') as f:
        try:
            while True:
                line = next(f)
                print(line, end='')
        except StopIteration:
            pass
# manual_iter()

使用生成器创建新的迭代模式

def frange(start, stop, increment):
    x = start
    while x < stop:
        yield x
        x += increment
for n in frange(0, 4, 0.5):
    print(n)
print(list(frange(0, 1, 0.25)))

代理迭代

class Node:
    def __init__(self, value):
        self._value = value
        self._children = []
    def __repr__(self):
        return 'Node({!r})'.format(self._value)
    def add_child(self, node):
        self._children.append(node)
    def __iter__(self):  # 实现了迭代器对象
        return iter(self._children)
    def depth_first(self):
        return DepthFirstIterator(self)

定义迭代器处理过程中需要维护大量的状态信息

class DepthFirstIterator(object):
    '''Depth-first traversal'''
    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):
        # Return myself if just started; create an iterator for children
        if self._children_iter is None:
            self._children_iter = iter(self._node)
            return self._node
        # If processing a child, return its next item
        elif self._child_iter:
            try:
                nextchild = next(self._child_iter)
                return nextchild
            except StopIteration:
                self._child_iter = None
                return next(self)
        # Advance to the next child and start its iteration
        else:
            self._child_iter = next(self._children_iter).depth_first()
            return next(self)
root = Node(0)
child1 = Node(1)
child2 = Node(2)
root.add_child(child1)
root.add_child(child2)
for ch in root:
    print(ch)

反向迭代

# f = open('test.txt')
# for line in reversed(list(f)):  # 转换列表需要消耗大量内存
#     print(line, end='')
# 实现 __reversed__()
class Countdown:
    def __init__(self, start):
        self.start = start
    # Forward iterator
    def __iter__(self):
        n = self.start
        while n > 0:
            yield n
            n -= 1
    # Reverse iterator
    def __reversed__(self):
        n = 1
        while n <= self.start:
            yield n
            n += 1
# for r in reversed(Countdown(30)):
#     print(r)

带有外部状态的生成器函数

# 暴露给用户使用的外部状态值
class linehistory:
    def __init__(self, lines, histlen=3):
        self.lines = lines
        self.history = deque(maxlen=histlen)
    def __iter__(self):
        for lineno, line in enumerate(self.lines, 1):
            self.history.append((lineno, line))
            yield line
    def clear(self):
        self.history.clear()
# 打印含有python的histlen行文档
with open('test.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='')

迭代器切片

#  islice()返回一个可以生成指定元素的迭代器
def addone(n):
    while True:
        yield n
        n += 1
c = addone(0)
for x in itertools.islice(c, 10, 20):  # [begin,end)
    print(x)

# 跳过可迭代对象的开始部分
# dropwhile() 函数。使用时,你给它传递一个函数对象和一个可迭代对象。
# 它会返回一个迭代器对象,丢弃原有序列中直到函数返回 Flase 之前的所有元素,然后返回后面所有元素。
# with open('test.txt') as f:
#     for line in itertools.dropwhile(lambda line: line.startswith('#'), f):
#         print(line, end='')

顺序迭代合并

a = [1, 4, 7, 10]
b = [2, 5, 6, 11]
# for x in heapq.merge(a, b):
#     print(x)
# print(heapq.merge(a, b))  # 生成器
print(list(heapq.merge(a, b)))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柴寺仓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值