Here is a list where I would like to keep only the 'windows' where there are a group of number higher than 3, but if in any case inside this group you find a smaller number, you keep it.
Moreover, if there is an isolated high number (between zeros for example), we need to delete it.
l = [3.5, 0, 0, 0.5, 4, 10, 20, 3, 20, 10, 2, 0, 0, 3.5, 0, 2, 18, 15, 2, 14, 2, 0]
and the expected result :
[4, 10, 20, 3, 20, 10, 18, 15, 2, 14]
This program does it but I feel this it not very pythonic, could you think of any other way ?
l = [3.5, 0, 0, 0.5, 4, 10, 20, 3, 20, 10, 2, 0, 0, 3.5, 0, 2, 18, 15, 2, 14, 2, 0]
new_l = []
index = []
seuil = 3
for i, elt in enumerate(l):
if elt > seuil:
if (l[i-1] and l[i+1]) > 1:
new_l.append(elt)
index.append(i)
else:
pass
else:
try:
if (l[i-1] and l[i+1]) > seuil:
new_l.append(elt)
index.append(i)
except IndexError:
pass
print index
print new_l
indexlist? It is not used in the code itself. \$\endgroup\$indexlist only actually. Thenew_lis only to check if the process goes well. \$\endgroup\$