generate list of partially accumulated values
cesco
fd.calabrese at gmail.com
Sun Sep 16 05:56:04 EDT 2007
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
More information about the Python-list
mailing list