Cleaner idiom for text processing?
Peter Otten
__peter__ at web.de
Wed May 26 08:45:32 EDT 2004
Duncan Booth wrote:
> What happens if someone works out that izip can be made much faster by
> consuming its iterators from right to left instead of left to right? That
> isn't nearly as far fetched as reading ahead.
This would also affect the calling code, when the arguments are iterators
(just swapping arguments to simulate the effect of the proposed
optimization):
>>> ia, ib = iter(range(3)), iter([])
>>> zip(ia, ib)
[]
>>> ia.next()
1
>>> ia, ib = iter(range(3)), iter([])
>>> zip(ib, ia)
[]
>>> ia.next()
0
>>>
Optimizations that are visible from the calling code always seem a bad idea
and against Python's philosophy. I admit the above reusage pattern is not
very likely, though.
> Passing the same iterator multiple times to izip is a pretty neat idea,
> but I would still be happier if the documentation explicitly stated that
> it consumes its arguments left to right.
>From the itertools documentation:
"""
izip(*iterables)
Make an iterator that aggregates elements from each of the iterables. Like
zip() except that it returns an iterator instead of a list. Used for
lock-step iteration over several iterables at a time. Equivalent to:
def izip(*iterables):
iterables = map(iter, iterables)
while iterables:
result = [i.next() for i in iterables]
yield tuple(result)
"""
I'd say the "Equivalent to [reference implementation]" statement should meet
your request.
Peter
More information about the Python-list
mailing list