0

Suppose i have list of numbers [3, 51, 34]. I want to add to each element the sum of the previous elements and return a new list with these new values. So here the result would be [3, 54, 88]. How to do it in general on an arbitrary-sized input list? The last line of this code should work on the known size lists.

indices1 = range(len(list1))

indices1.sort(key=lambda x: list2[x])
list1 = map(lambda i: list1[i], indices1)
labelled = zip(list1, ascii_uppercase)
sorted_data = sorted(labelled, key=itemgetter(0)) 

labels = [pair[1] for pair in sorted_data]
newlist, = [ 0, list1[0], list1[1] + list1[2], list1[0] + list[1] + list[2]]
Will
  • 24,082
  • 14
  • 97
  • 108
user3549209
  • 21
  • 1
  • 2

5 Answers5

3

numpy.cumsum might be good choice for something like this.

In [1]: import numpy as np

In [2]: a = [3,51,34]

In [3]: np.cumsum(a)
Out[3]: array([ 3, 54, 88])
Akavall
  • 82,592
  • 51
  • 207
  • 251
  • i want to do some processing on the first list & the 2nd list will be the output and i tried this code on the IDE there is too many errors – user3549209 Apr 18 '14 at 14:51
  • @user3549209 Can you provide the input you were using? – Akavall Apr 18 '14 at 14:52
  • @user3549209, `np.cumsum(a)` returns a `np.array`, if you want a `list`, you could just do `list(np.cumsum(a))`. – Akavall Apr 18 '14 at 15:13
3

a simple reduce:

nums = [3,51,34]
reduce(lambda x, y: [y] if not x else x + [y + x[-1]], nums, None)
# [3, 54, 88]
njzk2
  • 38,969
  • 7
  • 69
  • 107
  • I see in your code that you are adding a [0] at the beginning of the result list. In which case, the reduce would be `reduce(lambda x, y: x + [y + x[-1]], nums, [0])` (prints `[0, 3, 54, 88]`) – njzk2 Apr 18 '14 at 15:01
  • i am not adding a 0 actually the first element is set to zero so? – user3549209 Apr 18 '14 at 15:37
  • I deduced that from `newlist, = [ 0, ...`, but if that's incorrect, just ignore it. – njzk2 Apr 18 '14 at 15:39
  • because in your original code, your are effectively adding a 0 at the beginning of your output list. `list1` contains 3 elements, `newList` contains 4, the first being a 0. that's why. – njzk2 Apr 18 '14 at 15:43
1

A list comprehension approach, because that's always fun:

>>> data = [3, 51, 34]
>>> result = [n + sum(data[:i]) for i, n in enumerate(data)]
>>> result
[3, 54, 88]
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
0

And a trivial, procedural solution, for completeness:

a = [3,51,34]

def cumsum(numbers):
  accum = 0
  result = []
  for n in numbers:
    accum += n
    result.append(accum)
  return result

print cumsum(a)

It prints [3, 54, 88]

9000
  • 39,899
  • 9
  • 66
  • 104
0

generator based solution

In [25]: ll = [3,51,34]

In [26]: def acc(myiter):                                                                                                                                   
   ....:     it = iter(myiter)
   ....:     total = it.next()
   ....:     yield total
   ....:     for element in it:
   ....:         total = total + element                                                                                                                    
   ....:         yield total
   ....: 

In [27]: acc(ll)
Out[27]: <generator object acc at 0x1ec9e10>

In [28]: [x for x in acc(ll)]                                                                                                                               
Out[28]: [3, 54, 88]

as it will be the fastest one:

In [29]: %timeit [x for x in acc(ll)]
1000000 loops, best of 3: 1.26 µs per loop

In [30]: import numpy as np

In [31]: np.cumsum(ll)                                                                                                                                      
Out[31]: array([ 3, 54, 88])

In [32]: %timeit np.cumsum(ll)
100000 loops, best of 3: 15.8 µs per loop

In [33]: %timeit reduce(lambda x, y: [y] if not x else x + [y + x[-1]], ll, None)                                                                           
1000000 loops, best of 3: 1.87 µs per loop                                                                                                                  

In [34]: %timeit  [n + sum(ll[:i]) for i, n in enumerate(ll)]                                                                                               
100000 loops, best of 3: 1.99 µs per loop 
namit
  • 6,780
  • 4
  • 35
  • 41