itertools.tee是如何工作的,是否可以复制'itertools.tee'类型以保存其“状态”?

2024-05-12 18:54:09 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是关于itertools.tee的一些测试:

    li = [x for x in range(10)]
    ite = iter(li)
==================================================
    it = itertools.tee(ite, 5)
    >>> type(ite)
    <type 'listiterator'>
    >>> type(it)
    <type 'tuple'>
    >>> type(it[0])
    <type 'itertools.tee'>
    >>> 

    >>> list(ite)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(it[0])          # here I got nothing after 'list(ite)', why?
    []
    >>> list(it[1])
    []
====================play again===================
    >>> ite = iter(li)
    it = itertools.tee(ite, 5)
    >>> list(it[1])
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(it[2])
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(it[3])
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(it[4])
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(ite)
    []                       # why I got nothing? and why below line still have the data?   
    >>> list(it[0])
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(it[0])
    []
====================play again===================    
    >>> ite = iter(li)
    itt = itertools.tee(it[0], 5)    # tee the iter's tee[0].
    >>> list(itt[0])
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(itt[1])
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(it[0])
    []                               # why this has no data?
    >>> list(it[1])
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(ite)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  

我的问题是

  1. tee是如何工作的,为什么有时原始iter“有数据”而其他时间却没有?
  2. 我能把iter的深层副本作为“状态种子”来保存原始迭代器的状态并在以后使用吗?
  3. 我可以交换2个iter或2itertools.tee

谢谢!


Tags: theplaytypeitlilistittitertools
1条回答
网友
1楼 · 发布于 2024-05-12 18:54:09

tee接管原始迭代器;一旦启动迭代器,就放弃原始迭代器,因为它是tee的所有者(除非您真的知道自己在做什么)。

可以使用copy模块复制tee:

import copy, itertools
it = [1,2,3,4]
a, b = itertools.tee(it)
c = copy.copy(a)

。。。或者通过调用a.__copy__()

注意tee的工作方式是跟踪原始迭代器中使用的所有迭代值,这些值可能仍然被副本使用。

例如

a = [1,2,3,4]
b, c = itertools.tee(a)
next(b)

此时,位于bc下的tee对象读取了一个值1。它将它存储在内存中,因为它必须在迭代c时记住它。它必须将每个值保存在内存中,直到被tee的所有副本使用为止。

其结果是,您需要通过复制tee来小心“保存状态”。如果您实际上没有使用“saved state”tee中的任何值,那么您将导致tee将迭代器返回的每个值都永远保存在内存中(直到丢弃并收集复制的tee)。

相关问题 更多 >