Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List comprehension with an accumulator

What is the best way to replicate this simple function using a list comprehension (or another compact approach)?

import numpy as np

sum=0
array=[]
for i in np.random.rand(100):
   sum+=i
   array.append(sum)
like image 1000
Pierz Avatar asked Nov 26 '13 16:11

Pierz


People also ask

What is accumulator list in Python?

The accumulator pattern is used to collect information from a sequence and store the result in a variable. Core to the idea is that we initiate an accumulator variable (in the following examples called acc ) and modify that variable when iterating over the collection.

What is list comprehension explain with an example?

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Example: Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name.

Are list comprehensions good practice?

List comprehensions are great because they require less lines of code, are easier to comprehend, and are generally faster than a for loop. While list comprehensions are not the most difficult concept to grasp, it can definitely seem unintuitive at first (it certainly was for me!).

Can you do list comprehension with strings?

List comprehensions offer a succinct way to create lists based on existing lists. When using list comprehensions, lists can be built by leveraging any iterable, including strings and tuples. Syntactically, list comprehensions consist of an iterable containing an expression followed by a for clause.


1 Answers

In Python 3, you'd use itertools.accumulate():

from itertools import accumulate

array = list(accumulate(rand(100)))

Accumulate yields the running result of adding up the values of the input iterable, starting with the first value:

>>> from itertools import accumulate
>>> list(accumulate(range(10)))
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

You can pass in a different operation as a second argument; this should be a callable that takes the accumulated result and the next value, returning the new accumulated result. The operator module is very helpful in providing standard mathematical operators for this kind of work; you could use it to produce a running multiplication result for example:

>>> import operator
>>> list(accumulate(range(1, 10), operator.mul))
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

The functionality is easy enough to backport to older versions (Python 2, or Python 3.0 or 3.1):

# Python 3.1 or before

import operator

def accumulate(iterable, func=operator.add):
    'Return running totals'
    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
    it = iter(iterable)
    total = next(it)
    yield total
    for element in it:
        total = func(total, element)
        yield total
like image 54
Martijn Pieters Avatar answered Oct 06 '22 13:10

Martijn Pieters