Re: best cumulative sum




"Michael Spencer" <mahs@xxxxxxxxxxxxxxxxx> wrote in message
news:mailman.1054.1132707811.18701.python-> This can be written more
concisely as a generator:
>
> >>> import operator
> >>> def ireduce(func, iterable, init):
> ... for i in iterable:
> ... init = func(init, i)
> ... yield init

OK, this might do it. But is a generator "better"?
(I assume accuracy is the same, so what about speed?)

def ireduce(func, iterable, init=None):
if not init:
iterable = iter(iterable)
init = iterable.next()
yield init
elif not iterable:
yield init
for item in iterable:
init = func(init, item)
yield init

Alan Isaac


.



Relevant Pages

  • Re: Permutation Generator
    ... > this: a generator which returns all permutations of a list: ... Alan Isaac ... Prev by Date: ...
    (comp.lang.python)
  • Re: Battery jump starter with built in air pump any good?
    ... But when you have an RV you most always have more than adequate extra ... battery and often a generator too. ... Prev by Date: ...
    (rec.outdoors.rv-travel)
  • Re: Random Numbers
    ... only for exemplifying that the pseudo random number generator was fully ... Prev by Date: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: {S} Amazing luck!
    ... >>wasn't a typo. ... It is called the Random Number Generator, ... Must not have been so lucky after all... ... Prev by Date: ...
    (rec.games.roguelike.nethack)
  • Re: Random numbers in 5mx Sheet
    ... Every 'random number generator' starts with a seed number. ... always the same then the sequence will always be the same. ... Moderation is for monks." ... Prev by Date: ...
    (comp.sys.psion.apps)