欢迎光临
我们一直在努力

Python迭代器与生成器:从入门到精通

好的,我们将通过以下结构逐步理解Python迭代器与生成器:

一、理解基础循环

Python的for循环本质上是通过迭代协议实现的:

my_list = [1, 2, 3]
for item in my_list:
print(item)

等价于:

iterator = iter(my_list)
while True:
try:
item = next(iterator)
print(item)
except StopIteration:
break

https://www.iqiyi.com/v_14ewcy7th70.html
https://www.iqiyi.com/v_19gju8dquf4.html
https://www.iqiyi.com/v_1c4exe68c44.html
https://www.iqiyi.com/v_k1h1qt57as.html
https://www.iqiyi.com/v_jnkg4uw30c.html
http://www.iqiyi.com/v_1ibls0wnxqk.html
https://www.iqiyi.com/v_27z13tjk8ms.html
https://www.iqiyi.com/v_1d511pw472w.html
https://www.iqiyi.com/v_1qbdk73b10o.html
https://www.iqiyi.com/v_og300sfu5c.html
http://www.iqiyi.com/v_zzcwqs5i0s.html
https://www.iqiyi.com/v_12q9o9nliho.html
https://www.iqiyi.com/v_dj20koopj8.html
https://www.iqiyi.com/v_1t4xouenpg8.html
https://www.iqiyi.com/v_hk9rfuj5mg.html
https://www.iqiyi.com/v_1n1kb2gqtic.html
https://www.iqiyi.com/v_253szfsrpy8.html
https://www.iqiyi.com/v_1r9rq931uw0.html
https://www.iqiyi.com/v_1rjeyeh2lng.html
https://www.iqiyi.com/v_17p05maw5ok.html
https://www.iqiyi.com/v_lvaeugnfdk.html
https://www.iqiyi.com/v_1aju6g7o7i0.html
https://www.iqiyi.com/v_29298rjigzc.html
https://www.iqiyi.com/v_lqqynz7e98.html
https://www.iqiyi.com/v_mgg591e6c0.html
https://www.iqiyi.com/v_pg9ggs2onw.html
https://www.iqiyi.com/v_14nugl3kwms.html
https://www.iqiyi.com/v_1px5vpdo5dk.html
https://www.iqiyi.com/v_1buwphcbgmw.html
https://www.iqiyi.com/v_ivv44fsj1k.html

二、迭代器协议

符合迭代器协议的对象需满足两个条件:

  • 实现__iter__()方法(返回自身)
  • 实现__next__()方法(返回下一项)
  • class Counter:
    def __init__(self, n):
    self.n = n
    self.current = 0

    def __iter__(self):
    return self

    def __next__(self):
    if self.current < self.n:
    self.current += 1
    return self.current
    raise StopIteration

    # 使用示例
    for num in Counter(5):
    print(num) # 输出1到5

    三、生成器精要

    生成器是创建迭代器的便捷工具,使用yield关键字:

    def fibonacci(max_len):
    a, b = 0, 1
    count = 0
    while count < max_len:
    yield a
    a, b = b, a + b
    count += 1

    # 实际应用
    for num in fibonacci(10):
    print(num)

    https://www.iqiyi.com/v_14ewcy7th70.html
    https://www.iqiyi.com/v_19gju8dquf4.html
    https://www.iqiyi.com/v_1c4exe68c44.html
    https://www.iqiyi.com/v_k1h1qt57as.html
    https://www.iqiyi.com/v_jnkg4uw30c.html
    http://www.iqiyi.com/v_1ibls0wnxqk.html
    https://www.iqiyi.com/v_27z13tjk8ms.html
    https://www.iqiyi.com/v_1d511pw472w.html
    https://www.iqiyi.com/v_1qbdk73b10o.html
    https://www.iqiyi.com/v_og300sfu5c.html
    http://www.iqiyi.com/v_zzcwqs5i0s.html
    https://www.iqiyi.com/v_12q9o9nliho.html
    https://www.iqiyi.com/v_dj20koopj8.html
    https://www.iqiyi.com/v_1t4xouenpg8.html
    https://www.iqiyi.com/v_hk9rfuj5mg.html
    https://www.iqiyi.com/v_1n1kb2gqtic.html
    https://www.iqiyi.com/v_253szfsrpy8.html
    https://www.iqiyi.com/v_1r9rq931uw0.html
    https://www.iqiyi.com/v_1rjeyeh2lng.html
    https://www.iqiyi.com/v_17p05maw5ok.html
    https://www.iqiyi.com/v_lvaeugnfdk.html
    https://www.iqiyi.com/v_1aju6g7o7i0.html
    https://www.iqiyi.com/v_29298rjigzc.html
    https://www.iqiyi.com/v_lqqynz7e98.html
    https://www.iqiyi.com/v_mgg591e6c0.html
    https://www.iqiyi.com/v_pg9ggs2onw.html
    https://www.iqiyi.com/v_14nugl3kwms.html
    https://www.iqiyi.com/v_1px5vpdo5dk.html
    https://www.iqiyi.com/v_1buwphcbgmw.html
    https://www.iqiyi.com/v_ivv44fsj1k.html

    四、执行流程精解

    graph LR
    A[调用生成器函数] –> B[创建生成器对象]
    B –> C[调用next时运行]
    C –> D[执行至yield暂停]
    D –> E[返回yield值]
    E –> C

    五、应用对比场景

    特性传统迭代器生成器
    内存占用 存储完整序列 $$O(1)$$
    代码复杂度 需完整类实现 函数即定义
    状态保存 显式属性维护 自动暂停机制

    # 生成器表达式示例
    squared_gen = (x**2 for x in range(1000000)) # 仅需数KB内存

    六、进阶实战

    处理无限流数据:

    def real_time_data_stream():
    while True:
    data = get_network_data() # 假设的获取函数
    yield processed_data

    通过逐步理解这些核心概念,您将掌握Python迭代模型的精髓,并能灵活运用生成器处理海量数据流等现代应用场景。

    赞(0)
    未经允许不得转载:171主机测评 » Python迭代器与生成器:从入门到精通
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址