Check current index and next index equal to zero python

3

I need to walk through a list in Python, for example:

l = [2313, 1221, 1333, 66, 0, 0, 0, 0]

I would like if the current and next element in the list are equal to zero     replaces the current element and the next element by 1.

Or if the current and the previous element are zero:     replaces the current and previous element with 1.

How could I do this?

    
asked by anonymous 19.06.2016 / 02:16

1 answer

1

You can do this:

l = [0, 0, 2313, 1221, 0, 1333, 66, 0, 0, 0, 0]
lCount = len(l)
next1 = False

for i in range(0, lCount-1):
    if(l[i] == 0 and l[i+1] == 0):
        l[i] = 1
        next1 = True
    elif(next1):
        l[i] = 1
        next1 = False

if(next1): # ultimo elemento caso seja 0 seguido de outro (next1 definido no ultimo loop do ciclo)
    l[-1] = 1

print(l) # [1, 1, 2313, 1221, 0, 1333, 66, 1, 1, 1, 1]
    
19.06.2016 / 02:41