reduce()--what is it good for? (was: Re: reduce() anomaly?)
Alex Martelli
aleax at aleax.it
Wed Nov 12 07:21:29 EST 2003
David Eppstein wrote:
...
>> Wen seq is any iterable, all you need is izip(seq, islice(seq, 1, None)),
>> and you'll be creating no new list whatsoever. Still, tradeoffs in
>> obscurity (and performance for midsized lists) are quite as clear.
>
> If I'm not mistaken, this is buggy when seq is an iterable, and you need
Sorry, I should have said something like "re-iterable" -- an object such
that e.g.:
it1 = iter(seq)
val1 = it1.next()
it2 = iter(seq)
val2 = it2.next()
assert val1 == val2
holds (and keeps holding as you keen next'ing:-). list, tuple, dict, etc.
In particular, when the idiom zip(seq, seq[1:]) works, so should this one
(note in passing that, in said idiom, there is no need to slice the first
seq in the zip call to seq[:-1] -- zip truncates at the end of the
_shorter_ sequence anyway).
> to do something like
> seq1,seq2 = tee(seq)
> izip(seq1,islice(seq2,1,None))
> instead.
Yes, this is totally general. However, even though tee has now (2.4)
been implemented very smartly, this overall approach is still way
"conceptually heavy" (IMHO) when compared to, e.g.:
def window_by_2(it):
it = iter(it)
first = it.next()
for second in it:
yield first, second
fist = second
in any case, I do think that such 'windowing' is a general enough
need that it deserves its own optimized itertool...
Alex
More information about the Python-list
mailing list