Если вы используете ядро Python?
l = [0,1,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,1,0]
def split(l):
res = []
subres = [l[0]]
for i in range(len(l) - 1):
if l[i] == l[i + 1]:
subres.append(l[i + 1])
else:
res.append(subres)
subres = [l[i + 1]]
res.append(subres)
return(res)
def setall(l, val):
for i in range(len(l)):
l[i] = val
return(l)
def recode(l, changeval, replaceval, lenlimit):
for i in range(len(l) - 1):
el = l[i + 1]
if (el[0] == changeval) & (len(el) < lenlimit) & (i < (len(l) - 2)):
el = setall(el, replaceval)
l[i + 1] = el
return(l)
def flatten(l):
res = []
for el in l:
res.extend(el)
return(res)
# starting list
print(l)
# step 1
want1 = split(l)
want1 = flatten(recode(want1, 0, 1, 3))
print(want1)
# step 2
want2 = split(want1)
want2 = flatten(recode(want2, 1, 0, 4))
print(want2)
#[0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0]
#[0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0]
#[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]