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
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
...
>>>
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
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
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
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
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
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
> 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
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
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
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
12 matches
Mail list logo