I have the following list named "test_list":
test_list = [1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0,
1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0,
-1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0,
-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0,
-1.0, -1.0, -1.0, -1.0, 1.0]
I would like to iterate through the list, and:
- If a list element = 1;
- I would like to set the value of the next 5 elements in the list to 0; to factor-in a waiting period.
To do this I created the following helper function:
def adjust_for_wait_period(series_list, wait_period=5):
for i in series_list:
if i == 1:
index = series_list.index(i)
wait_period_list = list(range(1, (wait_period + 1)))
for wait in wait_period_list:
try:
series_list[index + wait] = 0
except Exception as e:
pass
return series_list
When I execute the function, it only iterates through the first elements of the list and outputs this list:
[1.0, 0, 0, 0, 0, 0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0,
1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0,
-1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
-1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0,
-1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0]
Instead of this list:
[1.0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 0, -1.0,
1.0, 0, 0, 0, 0, 0, -1.0, 1.0, 0, 0, 0,
0, 0, -1.0, -1.0, -1.0, -1.0, 1.0, 0, 0, 0, 0,
0, -1.0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0,
0, 0, -1.0, 1.0, 0, 0, 0, 0, 0, -1.0, 1.0]
This means that my function is only iterating through the first bunch of values and not iterating through the rest of the list.
Where could I be going wrong?