ddd
ddd

Reputation: 167

Incrementally increase non zero elements in a list

if i got this list

a = [1,0,0,1,0,0,0,1]

and I want it turned into

a = [1,0,0,2,0,0,0,3]

Upvotes: 1

Views: 118

Answers (6)

timgeb
timgeb

Reputation: 78750

Setup for solution #1 and #2

from itertools import count

to_add = count()
a = [1,0,0,1,0,0,0,1]

Solution #1

>>> [x + next(to_add) if x else x for x in a]
[1, 0, 0, 2, 0, 0, 0, 3]

Solution #2, hacky but fun

>>> [x and x + next(to_add) for x in a]
[1, 0, 0, 2, 0, 0, 0, 3]

Setup for solution #3 and #4

import numpy as np
a = np.array([1,0,0,1,0,0,0,1])

Solution #3

>>> np.where(a == 0, 0, a.cumsum())
array([1, 0, 0, 2, 0, 0, 0, 3])

Solution #4 (my favorite one yet)

>>> a*a.cumsum()
array([1, 0, 0, 2, 0, 0, 0, 3])

All the cumsum solutions assume that the non-zero elements of a are all ones.


Timings:

# setup
>>> a = [1, 0, 0, 1, 0, 0, 0, 1]*1000
>>> arr = np.array(a)
>>> to_add1, to_add2 = count(), count()
# IPython timings @ i5-6200U CPU @ 2.30GHz (though only relative times are of interest)
>>> %timeit [x + next(to_add1) if x else x for x in a] # solution 1
669 µs ± 3.59 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit [x and x + next(to_add2) for x in a] # solution 2
673 µs ± 15.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit np.where(arr == 0, 0, arr.cumsum()) # solution 3
34.7 µs ± 94.2 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit arr = np.array(a); np.where(arr == 0, 0, arr.cumsum()) # solution 3 with array creation
474 µs ± 14.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit arr*arr.cumsum() # solution 4
23.6 µs ± 131 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit arr = np.array(a); arr*arr.cumsum() # solution 4 with array creation
465 µs ± 6.82 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Upvotes: 5

iGian
iGian

Reputation: 11193

Other option: a one liner list comprehension, no dependencies.

[ 0 if e == 0 else sum(a[:i+1]) for i, e in enumerate(a) ]

#=> [1, 0, 0, 2, 0, 0, 0, 3]

Upvotes: 0

Osman Mamun
Osman Mamun

Reputation: 2882

Using numpy cumsum or cumulative sum to replace 1's to sum of 1's

In [4]: import numpy as np
In [5]: [i if i == 0 else j for i, j in zip(a, np.cumsum(a))]
Out[5]: [1, 0, 0, 2, 0, 0, 0, 3]

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71610

Use a list comprehension for this:

print([a[i]+a[:i].count(1) if a[i]==1 else a[i] for i in range(len(a))])

Output:

[1, 0, 0, 2, 0, 0, 0, 3]

Loop version:

for i in range(len(a)):
    if a[i]==1:
        a[i]=a[i]+a[:i].count(1)

Upvotes: 1

gold_cy
gold_cy

Reputation: 14226

Here is how I would do it:

def increase(l):
    count = 0
    for num in l:
        if num == 1:
            yield num + count
            count += 1
        else:
            yield num

c = list(increase(a))

c

[1, 0, 0, 2, 0, 0, 0, 3]

Upvotes: 3

mdrichardson
mdrichardson

Reputation: 7241

So, you want to increase each 1 except for the first one, right?

How about:

a = [1,0,0,1,0,0,0,1]

current_number = 0

for i, num in enumerate(a):
    if num == 1:
        a[i] = current_number + 1
        current_number += 1

print(a)

>>> [1, 0, 0, 2, 0, 0, 0, 3]

Or, if you prefer:

current_number = 1

for i, num in enumerate(a):
    if num == 1:
        a[i] = current_number
        current_number += 1

Upvotes: 1

Related Questions