A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2003-October/039037.html below:

[Python-Dev] Re: Reiterability

[Python-Dev] Re: ReiterabilityGuido van Rossum guido at python.org
Sun Oct 19 20:23:12 EDT 2003
> 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/)

More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4