I have a list with the following number:
[0, 0, 0, 0, 1, 1, 0, 0, 1]
What I wanna do is count how many consecutive 1s I have and generate a new list like the following one:
[0, 0, 0, 0, 1, 2, 0, 0, 1]
Is there a way to do it in a more compact way than this method?
lst = [0, 0, 0, 0, 1, 1, 0, 0, 1]
new_lst = []
counter = 0
for i in range(len(lst)):
if (lst[i] == 1): counter+=1
else:counter = 0
new_lst.append(counter)
print(new_lst)