function:
truths = (function(x) for x in string)
Then you can match them with 0s and 1s:
onesandzeroes = (1 if function(x) else 0 for x in string)
And then accumulate them:
running = itertools.accumulate(1 if function(x) else 0 for x in string)
As docs notes, it accumulatewas added in Python 3.2. If you are using 2.x, you can copy and paste the “Equivalent” recipe into documents. (If you are using 3.0-3.1, you can do the same, but in fact, in this case, just upgrade.)
source
share