Assuming I have n = 3
lists of same length for example:
R1 = [7,5,8,6,0,6,7]
R2 = [8,0,2,2,0,2,2]
R3 = [1,7,5,9,0,9,9]
I need to find the first index t
that verifies the n = 3 following conditions for a period p = 2
.
Edit: the meaning of period p
is the number of consecutive "boxes".
R1[t] >= 5, R1[t+1] >= 5
. Heret +p -1 = t+1
, we need to only verify for two boxest
andt+1
. Ifp
was equal to3
we will need to verify fort
,t+1
andt+2
. Note that It's always the same number for which we test, we always test if it's greater than5
for every index. The condition is always the same for all the "boxes".R2[t] >= 2, R2[t+1] >= 2
R3[t] >= 9, R3[t+1] >= 9
In total there is 3 * p conditions.
Here the t
I am looking for is 5
(indexing is starting from 0).
The basic way to do this is by looping on all the indexes using a for
loop. If the condition is found for some index t
we store it in some local variable temp
and we verify the conditions still hold for every element whose index is between t+1
and t+p -1
. If while checking, we find an index that does not satisfy a condition, we forget about the temp
and we keep going.
What is the most efficient way to do this in Python if I have large lists (like of 10000 elements)? Is there a more efficient way than the for loop?