generate list of partially accumulated values

C

cesco

Hi,

I have the following list:
l = [1, 2, 3, 4]
and I'd like to obtain a list like the following:
l_partial_sum = [1, 3, 6, 10] (that is [1, 1+2, 1+2+3, 1+2+3+4])

Is there a simple way to accomplish this?

I came up with the following solution but it seems a bit too
elaborated for the task:
l_partial_sum = [reduce(lambda x,y:x+y, sub_l) for sub_l in [l[:i+1]
for i in range(len(l))]]

Any help will be appreciated.

Thanks
Francesco
 
B

buffi

Hi,

I have the following list:
l = [1, 2, 3, 4]
and I'd like to obtain a list like the following:
l_partial_sum = [1, 3, 6, 10] (that is [1, 1+2, 1+2+3, 1+2+3+4])

Is there a simple way to accomplish this?

I came up with the following solution but it seems a bit too
elaborated for the task:
l_partial_sum = [reduce(lambda x,y:x+y, sub_l) for sub_l in [l[:i+1]
for i in range(len(l))]]

Any help will be appreciated.

Thanks
Francesco
l = [1, 2, 3, 4]
[sum(l[:x+1]) for x in xrange(len(l))]
[1, 3, 6, 10]

- Björn Kempén
 
B

buffi

Uh... that turned out weird.

Anyways, here goes again

l = [1, 2, 3, 4]
[sum(l[:x+1]) for x in xrange(len(l))]
 
C

cesco

l = [1, 2, 3, 4]
[sum(l[:x+1]) for x in xrange(len(l))]

Thanks,

actually my problem is a bit more complex (I thought I could get a
solution by posting a simplified version...)

The list is composed of objects:
l = [obj1, obj2, obj3, obj4]
and I need to call a method (say method1) on each object as follow:
l1 = [obj1.method1(obj2), obj2.method1(obj3), obj3.method1(obj4),
obj4]

Is there a clean way of doing this?

Thanks again
Francesco
 
Z

ZeD

cesco said:
The list is composed of objects:
l = [obj1, obj2, obj3, obj4]
and I need to call a method (say method1) on each object as follow:
l1 = [obj1.method1(obj2), obj2.method1(obj3), obj3.method1(obj4),
obj4]

to me it sounds a bit different from the original request, but...
Is there a clean way of doing this?

l = [obj1, obj2, obj3, obj4]
l1 = [a.method1(b) for a,b in zip(l, l[1:)] + l[-1:]
 
M

marek.rocki

Those methods of computing partial sums seem to be O(n^2) or worse.
What's wrong with an ordinary loop?

for i in xrange(1, len(l)):
l += l[i - 1]

And as for the list of objects:

ll = [l[i - 1].method(l) for i in xrange(1, len(l))] + l[-1]
 
J

J. Cliff Dyer

ZeD said:
cesco wrote:

The list is composed of objects:
l = [obj1, obj2, obj3, obj4]
and I need to call a method (say method1) on each object as follow:
l1 = [obj1.method1(obj2), obj2.method1(obj3), obj3.method1(obj4),
obj4]

to me it sounds a bit different from the original request, but...
That's because it's the next problem on his homework assignment. See
also the thread entitled: "generating list of sub lists.

Cheers,
Cliff
 
S

Steven D'Aprano

Hi,

I have the following list:
l = [1, 2, 3, 4]
and I'd like to obtain a list like the following:
l_partial_sum = [1, 3, 6, 10]
(that is [1, 1+2, 1+2+3, 1+2+3+4])

Is there a simple way to accomplish this?

Yes, use a loop. Put it into a function:

def partial_sums(L):
if not L:
return []
ps = [None]*len(L)
ps[0] = L[0]
for i in xrange(1, len(L)):
ps = ps[i-1] + L
return ps
l = [1, 2, 3, 4]
partial_sums(l)
[1, 3, 6, 10]


Here's a one-liner that give the same result:

[sum(l[0:i]) for i in xrange(len(l)+1)][1:]


Which is better, the one-liner using a super-fast built-in function
written in C, or the multi-line function using just slow Python code?

(Hint: I wouldn't be asking the question if the "obvious" answer was
right.)

Measure, don't guess!
.... "from __main__ import partial_sums; l = range(1000)").timeit(1000)
1.3528358936309814
timeit.Timer("[sum(l[0:i]) for i in xrange(len(l)+1)][1:]",
.... "l = range(1000)").timeit(1000)
41.276183843612671

The one-liner is thirty times slower than the function. Can you explain
why?

(Hint: how many times does it add numbers? How many times does it copy
parts of the list?)
 
S

Steven D'Aprano

The list is composed of objects:
l = [obj1, obj2, obj3, obj4]
and I need to call a method (say method1) on each object as follow: l1 =
[obj1.method1(obj2), obj2.method1(obj3), obj3.method1(obj4), obj4]

Is there a clean way of doing this?

Yes, write a function that loops over the list doing what you want. Not
everything needs to be a one-liner.
 
S

Steven D'Aprano

Those methods of computing partial sums seem to be O(n^2) or worse.
What's wrong with an ordinary loop?

Haven't you heard? There's a global shortage of newlines, and programmers
have been asked to do their bit to conserve them.
 
P

Paul Rubin

cesco said:
I have the following list:
l = [1, 2, 3, 4]
and I'd like to obtain a list like the following:
l_partial_sum = [1, 3, 6, 10] (that is [1, 1+2, 1+2+3, 1+2+3+4])

Is there a simple way to accomplish this?

I won't show explicit code since you are apparently asking a homework
question. But Python style tends to prefer doing this sort of thing
imperatively, possibly using generator functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top