In python, can I lazily generate copies of an iterator using tee? -


i'm trying create iterator lazily creates (potentially infinitely many) copies of iterator. possible?

i know can create fixed finite number of copies doing

iter_copies = tee(my_iter, n=10) 

but breaks down if don't know n ahead of time or if n infinite.

i try along lines of

def inf_tee(my_iter):     while true:         yield tee(my_iter)[1] 

but documentation states after using tee on iterator original iterator can no longer used, won't work.


in case you're interested in application: idea create lazy unzip function, potentially use in pytoolz. current implementation can handle finite number of infinite iterators (which better plain zip(*seq)), not infinite number of infinite iterators. here's pull request if you're interested in details.

this barely touched upon in single example near bottom of itertools documentation, itertools.tee supports copying:

import itertools, copy  def infinite_copies(some_iterable):     master, copy1 = itertools.tee(some_iterable)     yield copy1     while true:         yield copy.copy(master) 

the example in documentation uses __copy__ magic method, hook used customize copy.copy behavior.

note require storing every element ever produced original iterator, can expensive. there no way avoid cost.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -