Question

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]]
Was it helpful?

Solution

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]

OTHER TIPS

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])

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]

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]

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 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top