Re: best cumulative sum
- From: "David Isaac" <aisaac0@xxxxxxxxxxx>
- Date: Mon, 28 Nov 2005 18:05:05 GMT
"Peter Otten" <__peter__@xxxxxx> wrote in message
news:dmcvtp$s6s$02$1@xxxxxxxxxxxxxxxxxxxx
> 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-reduce shd be the
result of reduce on seq[:i] with the given initializer.
That said, for the applications I first intended,
yes it is sufficiently similar. For now, I'll stick
with the version below.
Thanks,
Alan
def ireduce(func, iterable, init=None):
iterable = iter(iterable)
if init is None:
init = iterable.next()
yield init
else:
try:
init = func(init, iterable.next())
yield init
except StopIteration:
yield init
for item in iterable:
init = func(init, item)
yield init
.
- 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
- Re: best cumulative sum
- From: bonono@xxxxxxxxx
- Re: best cumulative sum
- From: David Isaac
- Re: best cumulative sum
- From: Peter Otten
- Re: best cumulative sum
- From: David Isaac
- Re: best cumulative sum
- From: Peter Otten
- Re: best cumulative sum
- From: David Isaac
- Re: best cumulative sum
- From: Peter Otten
- Re: best cumulative sum
- From: David Isaac
- Re: best cumulative sum
- From: Peter Otten
- Re: best cumulative sum
- From: David Isaac
- Re: best cumulative sum
- From: Peter Otten
- best cumulative sum
- Prev by Date: Re: Precision for equality of two floats?
- Next by Date: Re: General question about Python design goals
- Previous by thread: Re: best cumulative sum
- Next by thread: Re: best cumulative sum
- Index(es):
Relevant Pages
|