Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list comprehension [duplicate]

Possible Duplicate:
List comprehension for running total

I'm trying to write a concise list comprehension statement to create a cdf: For example:

print f([0.2, 0.3,0.1,0.4])
[0.2,0.5,0.6,1.0] 

A standard procedure would look like this (I want to write a list comprehension for the function f()):

def f(probabilities) :

    sum = 0
    returnList = []
    for count in probabilities:
        sum +=count
        returnList = returnList + [sum]
    return returnList

Edit: I found a function numpy.cumsum(). I'll check if it uses list comprehensions.

like image 756
GeneralBecos Avatar asked Jan 25 '11 20:01

GeneralBecos


2 Answers

That operation is so common that many languages (mainly functional ones, but not only) provide abstractions for it, usually with the name scanl (it's like a reduce with intermediate results). Let's call it ireduce ("iterative reduce"):

def ireduce(f, state, it):
    for x in it:
        state = f(state, x)
        yield state

And now use it:

import operator

def f(probabilities):
    return ireduce(operator.add, 0, probabilities)

print(list(f([0.2, 0.3,0.1,0.4])))
# [0.2, 0.5, 0.6, 1.0]
like image 86
tokland Avatar answered Sep 30 '22 10:09

tokland


[sum(probabilities[:i+1]) for i in range(len(probabilities))]

But don't do that because it's O(n^2). Python list comprehensions weren't designed for this. Use the procedural code that you already wrote.

like image 31
novalis Avatar answered Sep 30 '22 12:09

novalis