best cumulative sum

David Isaac aisaac0 at verizon.net
Thu Nov 24 11:08:02 EST 2005


"Peter Otten" <__peter__ at web.de> wrote in message
news:dm3uj4$kko$02$1 at news.t-online.com...
> I'd rather have a second look whether the test is really needed.

That's too obscure of a hint.
Can you be a bit more explicit?
Here's an example (below).
You're saying I think that most of it is unnecessary.
Thanks,
Alan

def ireduce(func, iterable, init=None):
    iterable = iter(iterable)
    if init is None:
        init = iterable.next()
        yield init
    else:
        try:
            first = iterable.next()
            init = func(init, first)
            yield init
        except StopIteration:
            yield init
    for item in iterable:
        init = func(init, item)
        yield init





More information about the Python-list mailing list