以下是关于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]
我的问题是
itertools.tee
?谢谢!
tee
接管原始迭代器;一旦启动迭代器,就放弃原始迭代器,因为它是tee的所有者(除非您真的知道自己在做什么)。可以使用
copy
模块复制tee:。。。或者通过调用
a.__copy__()
。注意
tee
的工作方式是跟踪原始迭代器中使用的所有迭代值,这些值可能仍然被副本使用。例如
此时,位于
b
和c
下的tee对象读取了一个值1
。它将它存储在内存中,因为它必须在迭代c
时记住它。它必须将每个值保存在内存中,直到被tee的所有副本使用为止。其结果是,您需要通过复制tee来小心“保存状态”。如果您实际上没有使用“saved state”tee中的任何值,那么您将导致tee将迭代器返回的每个值都永远保存在内存中(直到丢弃并收集复制的tee)。
相关问题 更多 >
编程相关推荐