I'm attempting to write a function that given a list will update the 0 values to contain the nearest non zero value. If zeros are at beginning of list then the closest non zero value should be used to replace the zero value.
In other words the following list :
[0 , 0 , 10 , 25 , 30 , 0 , 0 , 0, 55 , 55 , 55 , 55 , 60 , 60 , 60 , 60]
should be updated to :
[10 , 10 , 10 , 25 , 30 , 30 , 30, 30, 55 , 55 , 55 , 55 , 60 , 60 , 60 , 60]
Here is the code I've written so far :
data = [0 , 0 , 10 , 25 , 30 , 0 , 0 , 0, 55 , 55 , 55 , 55 , 60 , 60 , 60 , 60]
index_to_update = []
for index, item in enumerate(data) :
updated_value = None
if item == 0 :
index_to_update.append(index)
else :
updated_value = item
break
for index in index_to_update :
data[index] = updated_value
updated_list = data.copy()
for index, item in enumerate(data) :
if item == 0 :
updated_list[index] = data[index - 1]
print(updated_list)
which prints :
[10, 25, 30, 55, 55, 55, 60, 60, 0, 60, 60, 0, 0]
If I use a list [0 , 0 , 10 , 25 , 30 , 0 , 55 , 55 , 55 , 55 , 60 , 60 , 60 , 60] then it works as expected :
data = [0 , 0 , 10 , 25 , 30 , 0 , 55 , 55 , 55 , 55 , 60 , 60 , 60 , 60]
index_to_update = []
for index, item in enumerate(data) :
updated_value = None
if item == 0 :
index_to_update.append(index)
else :
updated_value = item
break
for index in index_to_update :
data[index] = updated_value
updated_list = data.copy()
for index, item in enumerate(data) :
if item == 0 :
updated_list[index] = data[index - 1]
print(updated_list)
prints :
[10, 10, 10, 25, 30, 30, 55, 55, 55, 55, 60, 60, 60, 60]
0's at positions 0 and 1 are replaced with 10. 0 at position 5 is replaced with 30.
My algorithm breaks when there are multiple 0's contained :
[0 , 0 , 10 , 25 , 30 , 0 , 0 , 0, 55 , 55 , 55 , 55 , 60 , 60 , 60 , 60]
What modification(s) is required to my algo in order to convert a list containing elements
[0 , 0 , 10 , 25 , 30 , 0 , 0 , 0, 55 , 55 , 55 , 55 , 60 , 60 , 60 , 60]
to :
[10 , 10 , 10 , 25 , 30 , 30 , 30 , 30, 55 , 55 , 55 , 55 , 60 , 60 , 60 , 60]