Prev: how to copy and move file with its attribute?
Next: CPython 3.1.1 docs error in noddy3 example?
From: Steven D'Aprano on 19 Jul 2010 10:49 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, 10 ] [pedant] The above are *lists*, not arrays. Python has arrays, but you have to call "import array" to get them. [/pedant] > What is the best way (or pythonic way) to get this. Others have given you a plethora of advanced, complicated and obscure ways to solve this question, but I haven't seen anyone give the simplest method (simple as in no tricks or advanced features): data = [0, 1, 2, 1, 1, 0, 0, 2, 3] csums = [] for x in data: if csums: y = x + csums[-1] else: y = x csums.append(y) We can save some code with the ternary operator: data = [0, 1, 2, 1, 1, 0, 0, 2, 3] csums = [] for x in data: csums.append((x + csums[-1]) if csums else x) Here's a version that writes the cumulative sum in place: data = [0, 1, 2, 1, 1, 0, 0, 2, 3] for i in range(1, len(data)): data[i] += data[i-1] -- Steven
From: Brian Victor on 19 Jul 2010 12:12 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 pythonic way) to get this. Now that Steven's given you the simple, pythonic way, I'll just mention the advanced, complicated and obscure way that might be vaguely familiar if you're coming from a functional programming background: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] def running_sum(result, current_value): return result + [result[-1]+current_value if result else current_value] reduce(running_sum, x, []) Having offered this, I don't recall ever seeing reduce used in real python code, and explicit iteration is almost always preferred. -- Brian
From: dhruvbird on 19 Jul 2010 12:56 On Jul 19, 9:12 pm, Brian Victor <homeusen...(a)brianhv.org> 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, 3, 4, 5, 5, 5, 7, 10 ] > > What is the best way (or pythonic way) to get this. > > Now that Steven's given you the simple, pythonic way, I'll just mention > the advanced, complicated and obscure way that might be vaguely familiar > if you're coming from a functional programming background: > > x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] > def running_sum(result, current_value): > return result + [result[-1]+current_value if result else current_value] > > reduce(running_sum, x, []) > > 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 function. So, I guess no function like "accumulate" below exists in the standard lib. def accumulate(proc, seed, seq): ret = [] for i in seq: ret.append(proc(seed, i)) return ret x = [0, 1, 3, 4, 5, 5, 5, 7, 10] print accumulate(lambda x,y: x+y, 0, x) My guess is that accumulate can be used in many more scenarios. Regards, -Dhruv.
From: dhruvbird on 19 Jul 2010 13:01 On Jul 19, 4:28 pm, Peter Otten <__pete...(a)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: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ] > > What is the best way (or pythonic way) to get this. > > Homework? not really :) It's just that I was wondering if a built-in function for doing such things (which I find myself doing increasingly with an explicit loop) exists. Regards, -Dhruv. > > >>> def cumulative_sum(values, start=0): > > ... for v in values: > ... start += v > ... yield start > ...>>> list(cumulative_sum([ 0, 1, 2, 1, 1, 0, 0, 2, 3 ])) > > [0, 1, 3, 4, 5, 5, 5, 7, 10] > > Peter
From: Duncan Booth on 19 Jul 2010 13:51 dhruvbird <dhruvbird(a)gmail.com> wrote: > On Jul 19, 4:28�pm, Peter Otten <__pete...(a)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: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ] >> > � What is the best way (or pythonic way) to get this. >> >> Homework? > > not really :) > > It's just that I was wondering if a built-in function for doing such > things (which I find myself doing increasingly with an explicit loop) > exists. > Why would you find yourself doing it more than once? Write it once in a function and then just re-use the code.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: how to copy and move file with its attribute? Next: CPython 3.1.1 docs error in noddy3 example? |