Re: best cumulative sum
- From: "bonono@xxxxxxxxx" <bonono@xxxxxxxxx>
- Date: 22 Nov 2005 17:11:05 -0800
Michael Spencer wrote:
> David Isaac wrote:
> for a solution when these are available.
> > Something like:
> > def cumreduce(func, seq, init = None):
> > """Return list of cumulative reductions.
> >
> >
> 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
> ...
> >>> list(ireduce(operator.mul, range(1,5),init=1))
> [1, 2, 6, 24]
> >>>
If iterable has no elements, I believe the behaviour should be [init],
there is also the case of init=None that needs to be handled. But
otherwise, that is more or less what I wrote for my scanl/scanl1.
> Michael
.
- Follow-Ups:
- Re: best cumulative sum
- From: David Isaac
- Re: best cumulative sum
- References:
- best cumulative sum
- From: David Isaac
- Re: best cumulative sum
- From: Micah Elliott
- Re: best cumulative sum
- From: Erik Max Francis
- Re: best cumulative sum
- From: bonono@xxxxxxxxx
- Re: best cumulative sum
- From: David Isaac
- Re: best cumulative sum
- From: Colin J. Williams
- Re: best cumulative sum
- From: David Isaac
- Re: best cumulative sum
- From: Michael Spencer
- best cumulative sum
- Prev by Date: Re: user-defined operators: a very modest proposal
- Next by Date: Re: defining the behavior of zip(it, it) (WAS: Converting a flat list...)
- Previous by thread: Re: best cumulative sum
- Next by thread: Re: best cumulative sum
- Index(es):