Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop to factor given list

I have data l:

[[Timestamp('2022-07-14 00:01:00'), Timestamp('2022-07-14 00:11:00'), 1],
 [Timestamp('2022-07-14 00:08:00'), Timestamp('2022-07-14 00:18:00'), 1],
 [Timestamp('2022-07-14 00:15:00'), Timestamp('2022-07-14 00:25:00'), 1],
 [Timestamp('2022-07-14 00:22:00'), Timestamp('2022-07-14 00:32:00'), 0],
 [Timestamp('2022-07-14 00:29:00'), Timestamp('2022-07-14 00:39:00'), 0],
 [Timestamp('2022-07-14 00:36:00'), Timestamp('2022-07-14 00:46:00'), 1],
 [Timestamp('2022-07-14 00:43:00'), Timestamp('2022-07-14 00:53:00'), 1],
 [Timestamp('2022-07-14 00:50:00'), Timestamp('2022-07-14 01:00:00'), 1],
 [Timestamp('2022-07-14 00:57:00'), Timestamp('2022-07-14 01:07:00'), 0],
 [Timestamp('2022-07-14 01:04:00'), Timestamp('2022-07-14 01:14:00'), 1],
 [Timestamp('2022-07-14 01:11:00'), Timestamp('2022-07-14 01:21:00'), 1],
 [Timestamp('2022-07-14 01:18:00'), Timestamp('2022-07-14 01:28:00'), 1],
 [Timestamp('2022-07-14 01:25:00'), Timestamp('2022-07-14 01:35:00'), 0],
 [Timestamp('2022-07-14 01:32:00'), Timestamp('2022-07-14 01:42:00'), 0]]

I need a loop to factor this data to obtain first timestamp where l[i][2] == 1 and second timestamp where l[i][2] is 1 consecutively in a row.

expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Timestamp('2022-07-14 00:01:00'), Timestamp('2022-07-14 00:25:00')
...

>Solution :

Try this.

start = None
end = None
for i in range(len(l)):
    if l[i][2] == 1:
        if start is None:
            start = l[i][0]
        end = l[i][1]
    elif start is not None:
        break

print(start, end)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading