"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
[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
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
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)
>
"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.
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
"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
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.
>
>>
"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
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
"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
"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.
"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)
> ...
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
[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]
> 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 "
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
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
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):
...
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
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
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
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
> 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
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
<[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
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/
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;
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
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
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
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
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
33 matches
Mail list logo