Re: Accumulate function in python

2010-08-02 Thread Aahz
In article <7xpqyjgvjm@ruckus.brouhaha.com>, Paul Rubin wrote: > >I think Peter Otten's solution involving a generator is the one most in >the current Python spirit. It's cleaner (for my tastes) than the ones >that use things like list.append. Agreed -- Aahz (a...@pythoncraft.com)

Re: Accumulate function in python

2010-07-27 Thread sturlamolden
On 19 Jul, 13:18, dhruvbird wrote: > Hello, >   I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] >   And would like to compute the cumulative sum of all the integers > from index zero into another array. So for the array above, I should > get: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ] >   What is

Re: Accumulate function in python

2010-07-27 Thread Stefan Behnel
geremy condra, 27.07.2010 12:54: On Wed, Jul 21, 2010 at 8:17 AM, John Nagle wrote: On 7/19/2010 9:56 AM, dhruvbird wrote: On Jul 19, 9:12 pm, Brian Victor wrote: dhruvbird wrote: Having offered this, I don't recall ever seeing reduce used in real python code, and explicit iteration is a

Re: Accumulate function in python

2010-07-27 Thread geremy condra
On Wed, Jul 21, 2010 at 8:17 AM, John Nagle wrote: > On 7/19/2010 9:56 AM, dhruvbird wrote: >> >> On Jul 19, 9:12 pm, Brian Victor  wrote: >>> >>> dhruvbird wrote: > >>> Having offered this, I don't recall ever seeing reduce used in real >>> python code, and explicit iteration is almost always pre

Re: Accumulate function in python

2010-07-26 Thread dhruvbird
On Jul 21, 8:17 pm, John Nagle wrote: > On 7/19/2010 9:56 AM, dhruvbird wrote: > > > On Jul 19, 9:12 pm, Brian Victor  wrote: > >> dhruvbird wrote: > >> Having offered this, I don't recall ever seeing reduce used in real > >> python code, and explicit iteration is almost always preferred. > > > Ye

Re: Accumulate function in python

2010-07-21 Thread John Nagle
On 7/19/2010 9:56 AM, dhruvbird wrote: On Jul 19, 9:12 pm, Brian Victor wrote: dhruvbird wrote: Having offered this, I don't recall ever seeing reduce used in real python code, and explicit iteration is almost always preferred. Yes, even I have noticed that reduce is a tad under-used funct

Re: Accumulate function in python

2010-07-19 Thread Joel Goldstick
Peter Otten wrote: dhruvbird wrote: I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] And would like to compute the cumulative sum of all the integers from index zero into another array. So for the array above, I should get: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ] What is the best way (

Re: Accumulate function in python

2010-07-19 Thread Paul Rubin
Brian Victor writes: > def running_sum(result, current_value): > return result + [result[-1]+current_value if result else current_value] > > reduce(running_sum, x, []) That is not really any good because Python lists are actually vectors, so result+[...] actually copies the whole old list, ma

Re: Accumulate function in python

2010-07-19 Thread Duncan Booth
dhruvbird wrote: > On Jul 19, 4:28 pm, Peter Otten <__pete...@web.de> wrote: >> dhruvbird wrote: >> >   I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] >> >   And would like to compute the cumulative sum of all the integers >> > from index zero into another array. So for the array ab

Re: Accumulate function in python

2010-07-19 Thread dhruvbird
On Jul 19, 4:28 pm, Peter Otten <__pete...@web.de> wrote: > dhruvbird wrote: > >   I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] > >   And would like to compute the cumulative sum of all the integers > > from index zero into another array. So for the array above, I should > > get: [

Re: Accumulate function in python

2010-07-19 Thread dhruvbird
On Jul 19, 9:12 pm, Brian Victor wrote: > dhruvbird wrote: > > Hello, > >   I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] > >   And would like to compute the cumulative sum of all the integers > > from index zero into another array. So for the array above, I should > > get: [ 0, 1,

Re: Accumulate function in python

2010-07-19 Thread Brian Victor
dhruvbird wrote: > Hello, > I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] > And would like to compute the cumulative sum of all the integers > from index zero into another array. So for the array above, I should > get: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ] > What is the best way (or p

Re: Accumulate function in python

2010-07-19 Thread Steven D'Aprano
On Mon, 19 Jul 2010 04:18:48 -0700, dhruvbird wrote: > Hello, > I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] And would > like to compute the cumulative sum of all the integers > from index zero into another array. So for the array above, I should > get: [ 0, 1, 3, 4, 5, 5, 5, 7

Re: Accumulate function in python

2010-07-19 Thread Andre Alexander Bell
On 07/19/2010 01:18 PM, dhruvbird wrote: > Hello, > I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] > And would like to compute the cumulative sum of all the integers > from index zero into another array. So for the array above, I should > get: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ] > Wh

Re: Accumulate function in python

2010-07-19 Thread Mick Krippendorf
Am 19.07.2010 13:18, dhruvbird wrote: > Hello, > I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] > And would like to compute the cumulative sum of all the integers > from index zero into another array. So for the array above, I should > get: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ] > What

Re: Accumulate function in python

2010-07-19 Thread Vlastimil Brom
2010/7/19 dhruvbird : > Hello, >  I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] >  And would like to compute the cumulative sum of all the integers > from index zero into another array. So for the array above, I should > get: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ] >  What is the best way (or

Re: Accumulate function in python

2010-07-19 Thread Peter Otten
dhruvbird wrote: > I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] > And would like to compute the cumulative sum of all the integers > from index zero into another array. So for the array above, I should > get: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ] > What is the best way (or pythonic

Re: Accumulate function in python

2010-07-19 Thread Nitin Pawar
Hi, you may want to do like this array=[0,1,2] sumArray = [] for element in range(0,len(array)): if element == 0 : sumArray.append(array[element]) else: sumArray.append((array[element] + sumArray[element-1])) and then you can recheck it Thanks, nitin On Mon, Jul 19, 20