[Python-Dev] Re: Reiterability
Guido van Rossum
guido at python.org
Sun Oct 19 20:23:12 EDT 2003
More information about the Python-Dev mailing list
Sun Oct 19 20:23:12 EDT 2003
- Previous message: [Python-Dev] Re: Reiterability
- Next message: [Python-Dev] Re: Reiterability
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> So far, all of my needs for re-iteration have been met by storing some
> of the iterator's data. If all of it needs to be saved, I use list(it).
> If only a portion needs to be saved, then I use the code from the tee()
> example in the itertools documentation:
>
> def tee(iterable):
> "Return two independent iterators from a single iterable"
> def gen(next, data={}, cnt=[0]):
> dpop = data.pop
> for i in itertools.count():
> if i == cnt[0]:
> item = data[i] = next()
> cnt[0] += 1
> else:
> item = dpop(i)
> yield item
> next = iter(iterable).next
> return (gen(next), gen(next))
Ouch. That required hard work to understand! :-) And it doesn't
generalize straightforwardly to three or more iterators.
This approach is nice if you expect the two iterators to remain close
together. But if they go far apart (without degenerating to the
list(it) case like Alex's example) I imagine that different data
structure than a dict would be more efficient to hold the queue.
--Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] Re: Reiterability
- Next message: [Python-Dev] Re: Reiterability
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list