Re: best cumulative sum

2005-11-28 Thread David Isaac
"Peter Otten" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > sufficiently similar I think I understand your points now. But I wanted to match these cases: >>> import operator >>> reduce(operator.add,[],42) 42 >>> reduce(operator.add,[1],42) 43 The idea is that the i-th yield of i

Re: best cumulative sum

2005-11-28 Thread Peter Otten
[EMAIL PROTECTED] wrote: >> def ireduce(op, iterable, *init): >> iterable = chain(init, iterable) >> accu = iterable.next() >> yield accu >> for item in iterable: >> accu = op(accu, item) >> yield accu > I believe there is only one initializer in reduce. Throw in

Re: best cumulative sum

2005-11-27 Thread [EMAIL PROTECTED]
Peter Otten wrote: > David Isaac wrote: > > > "Peter Otten" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > >> I think that the test for an empty iterator makes ireduce() unintuitive. > > > > OK. > > I misunderstood you point. > > But that is needed to match the behavior of reduc

Re: best cumulative sum

2005-11-27 Thread Peter Otten
David Isaac wrote: > "Peter Otten" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> I think that the test for an empty iterator makes ireduce() unintuitive. > > OK. > I misunderstood you point. > But that is needed to match the behavior of reduce. reduce(operator.add,[],42) >

Re: best cumulative sum

2005-11-27 Thread David Isaac
"Peter Otten" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I think that the test for an empty iterator makes ireduce() unintuitive. OK. I misunderstood you point. But that is needed to match the behavior of reduce. >>> reduce(operator.add,[],42) 42 Thanks, Alan -- http://mail.

Re: best cumulative sum

2005-11-26 Thread Peter Otten
David Isaac wrote: > "Peter Otten" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> 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 o

Re: best cumulative sum

2005-11-24 Thread David Isaac
"Peter Otten" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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

Re: best cumulative sum

2005-11-24 Thread Peter Otten
Alan aka David Isaac wrote: > "Peter Otten" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> You are in for a surprise here: > > You got that right! > >> >>> def empty(): >> ... for item in []: >> ... yield item >> ... >> >>> bool(empty()) >> True > > Ouch. > >>

Re: best cumulative sum

2005-11-23 Thread David Isaac
"Peter Otten" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > You are in for a surprise here: You got that right! > >>> def empty(): > ... for item in []: > ... yield item > ... > >>> bool(empty()) > True Ouch. > >>> bool(iter([])) > True # python 2.3 and probably

Re: best cumulative sum

2005-11-23 Thread Peter Otten
David Isaac wrote: > def ireduce(func, iterable, init=None): > if init is None: > iterable = iter(iterable) > init = iterable.next() > yield init > elif not iterable: You are in for a surprise here: >>> def empty(): ... for item in []: ... yield it

Re: best cumulative sum

2005-11-23 Thread David Isaac
"Peter Otten" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Of course nothing can beat a plain old for loop in terms of readability and > -- most likely -- speed. Here are two versions, meant to be comparable. Thanks, Alan Isaac def cumreduce(func, seq, init = None): cr = seq

Re: best cumulative sum

2005-11-23 Thread David Isaac
"Peter Otten" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > - allows arbitrary iterables, not sequences only > - smaller memory footprint if sequential access to the items is sufficient Sure; I meant aside from that. > - fewer special cases, therefore > - less error prone, e. g.

Re: best cumulative sum

2005-11-23 Thread David Isaac
"Michael Spencer" <[EMAIL PROTECTED]> 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) > ...

Re: best cumulative sum

2005-11-23 Thread [EMAIL PROTECTED]
David Isaac wrote: > OK, this might do it. But is a generator "better"? > (I assume accuracy is the same, so what about speed?) > I have the slightest idea. So long it works for me and is not too hard to understand and has no obvious speed problem, I don't care too much. I believe in line is in g

Re: best cumulative sum

2005-11-23 Thread Peter Otten
[David Isaac] > def cumreduce(func, seq, init = None): > if not seq: > cr = [init]*bool(init) > else: > cr = [seq[0]] * len(seq) > if init: > cr[0] = func(cr[0],init) > for idx in range(1,len(seq)): > cr[idx] = func(cr[idx-1],seq[idx]

Re: best cumulative sum

2005-11-23 Thread David Isaac
> Michael Spencer wrote: > > This can be written more concisely as a generator: <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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. Right. So it is "

Re: best cumulative sum

2005-11-23 Thread [EMAIL PROTECTED]
David Isaac wrote: > > Michael Spencer wrote: > > > This can be written more concisely as a generator: > > > <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > If iterable has no elements, I believe the behaviour should be [init], > > there is also the case of init=None that needs t

Re: best cumulative sum

2005-11-22 Thread [EMAIL PROTECTED]
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 > >>> de

Re: best cumulative sum

2005-11-22 Thread Michael Spencer
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): ...

Re: best cumulative sum

2005-11-22 Thread Paul Rubin
Here's a silly recursive version (don't really use, it's too slow): def csum(s): if len(s)==0: return s return csum(s[:-1]) + [sum(s)] -- http://mail.python.org/mailman/listinfo/python-list

Re: best cumulative sum

2005-11-22 Thread Gerard Flanagan
David Isaac wrote: > What's the good way to produce a cumulative sum? > E.g., given the list x, > cumx = x[:] > for i in range(1,len(x)): > cumx[i] = cumx[i]+cumx[i-1] > > What's the better way? > > Thanks, > Alan Isaac Don't know about better, but this is what I came up with: class PartialSum

Re: best cumulative sum

2005-11-22 Thread [EMAIL PROTECTED]
David Isaac wrote: > <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > He seems to want scanl > > Yes. But it's not in Python, right? > (I know about Keller's version.) > > Robert Kern wrote: > > Define better. More accurate? Less code? > > Good point. > As Bonono (?) suggested: I

Re: best cumulative sum

2005-11-21 Thread Bengt Richter
On Mon, 21 Nov 2005 15:23:20 +1100, Steven D'Aprano <[EMAIL PROTECTED]> wrote: >David (Alan) Isaac wrote: > >> What's the good way to produce a cumulative sum? >> E.g., given the list x, >> cumx = x[:] >> for i in range(1,len(x)): >> cumx[i] = cumx[i]+cumx[i-1] >> >> What's the better way? > >Is

Re: best cumulative sum

2005-11-21 Thread David Isaac
> Alan Isaac wrote: >> Like SciPy's cumsum. "Colin J. Williams" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Doesn't numarray handle this? Sure. One might say that numarray is in the process of becoming scipy. But I was looking for a solution when these are available. Something

Re: best cumulative sum

2005-11-21 Thread Colin J. Williams
David Isaac wrote: > <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >>He seems to want scanl > > > Yes. But it's not in Python, right? > (I know about Keller's version.) > > Robert Kern wrote: > >>Define better. More accurate? Less code? > > > Good point. > As Bonono (?) su

Re: best cumulative sum

2005-11-21 Thread David Isaac
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > He seems to want scanl Yes. But it's not in Python, right? (I know about Keller's version.) Robert Kern wrote: > Define better. More accurate? Less code? Good point. As Bonono (?) suggested: I'd most like a solution that relies on a

Re: best cumulative sum

2005-11-20 Thread [EMAIL PROTECTED]
Erik Max Francis wrote: > Micah Elliott wrote: > > > On Nov 21, David Isaac wrote: > > > >> What's the good way to produce a cumulative sum? > > > import operator > x = 1,2,3 > reduce(operator.add, x) > > 6 > > Or just sum(x). > He seems to want scanl -- http://mail.python.org/

Re: best cumulative sum

2005-11-20 Thread Robert Kern
Erik Max Francis wrote: > Micah Elliott wrote: > >>On Nov 21, David Isaac wrote: >>>What's the good way to produce a cumulative sum? >> >import operator >x = 1,2,3 >reduce(operator.add, x) >> >>6 > > Or just sum(x). That just gives you the tail end. The OP asked for a cumulative sum;

Re: best cumulative sum

2005-11-20 Thread Erik Max Francis
Micah Elliott wrote: > On Nov 21, David Isaac wrote: > >> What's the good way to produce a cumulative sum? > import operator x = 1,2,3 reduce(operator.add, x) > 6 Or just sum(x). -- Erik Max Francis && [EMAIL PROTECTED] && http://www.alcyone.com/max/ San Jose, CA, USA && 37 20

Re: best cumulative sum

2005-11-20 Thread Micah Elliott
On Nov 21, David Isaac wrote: > What's the good way to produce a cumulative sum? >>> import operator >>> x = 1,2,3 >>> reduce(operator.add, x) 6 -- _ _ ___ |V|icah |- lliott <>< [EMAIL PROTECTED] " " """ -- http://mail.python.org/mailman/listinfo/python-list

Re: best cumulative sum

2005-11-20 Thread Steven D'Aprano
David (Alan) Isaac wrote: > What's the good way to produce a cumulative sum? > E.g., given the list x, > cumx = x[:] > for i in range(1,len(x)): > cumx[i] = cumx[i]+cumx[i-1] > > What's the better way? Is there something that this doesn't do, or something it does do that it shouldn't? You cou

Re: best cumulative sum

2005-11-20 Thread Robert Kern
David Isaac wrote: > What's the good way to produce a cumulative sum? > E.g., given the list x, > cumx = x[:] > for i in range(1,len(x)): > cumx[i] = cumx[i]+cumx[i-1] > > What's the better way? Define better. More accurate? Less code? -- Robert Kern [EMAIL PROTECTED] "In the fields of hell w

best cumulative sum

2005-11-20 Thread David Isaac
What's the good way to produce a cumulative sum? E.g., given the list x, cumx = x[:] for i in range(1,len(x)): cumx[i] = cumx[i]+cumx[i-1] What's the better way? Thanks, Alan Isaac -- http://mail.python.org/mailman/listinfo/python-list