Re: generate list of partially accumulated values

2007-09-16 Thread Paul Rubin
cesco <[EMAIL PROTECTED]> writes: > 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 apparentl

Re: generate list of partially accumulated values

2007-09-16 Thread Vadim Radionov
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]) >>> def iscan(f, seq, acc=0): ... for i in seq: ...acc = f(acc,i) ...yield acc ... >>>

Re: generate list of partially accumulated values

2007-09-16 Thread Steven D'Aprano
On Sun, 16 Sep 2007 04:18:30 -0700, marek.rocki wrote: > 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. -- Ste

Re: generate list of partially accumulated values

2007-09-16 Thread Steven D'Aprano
On Sun, 16 Sep 2007 09:56:04 +, cesco wrote: > 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

Re: generate list of partially accumulated values

2007-09-16 Thread Steven D'Aprano
On Sun, 16 Sep 2007 10:21:59 +, 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] > > Is there a clean way of doing this

Re: generate list of partially accumulated values

2007-09-16 Thread J. Cliff Dyer
ZeD wrote: > 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

Re: generate list of partially accumulated values

2007-09-16 Thread 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[i] += l[i - 1] And as for the list of objects: ll = [l[i - 1].method(l[i]) for i in xrange(1, len(l))] + l[-1] -- http://mail.python.org/mailman/listinfo/py

Re: generate list of partially accumulated values

2007-09-16 Thread ZeD
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... > Is

Re: generate list of partially accumulated values

2007-09-16 Thread 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 obje

Re: generate list of partially accumulated values

2007-09-16 Thread 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))] -- http://mail.python.org/mailman/listinfo/python-list

Re: generate list of partially accumulated values

2007-09-16 Thread buffi
On Sep 16, 11:56 am, cesco <[EMAIL PROTECTED]> wrote: > 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 f

generate list of partially accumulated values

2007-09-16 Thread 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_par