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

How can I only look at only a certain range within a sorted list in Python?

I have sorted lists of starting times and ending times. I also have a very large sorted list of times, t.

Right now, I am doing the extremely slow:

for start, end in zip(startTimes, endTimes):

    for time in t:
 
        if start <= t <= end:
            do something

This looks at each element in t every single time. I know I can reduce this by making some elifs for whether or not t < start or end < t, but there must be a way to take advantage of the lists being sorted.

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

>Solution :

Suppose you have a 1,000,000 day interval in list t:

import datetime as dt 

st=dt.datetime(1990, 1, 1)
t=[st]
for i in range(1_000_000):
    t.append(t[-1]+dt.timedelta(days=1))

>>> t[-1]
4727-11-29 00:00:00   # if man is still alive...

You can use itertools groupby to find sub intervals:

from itertools import groupby   
x=dt.datetime(2040,2,2)
y=dt.datetime(2040,2,6) 
for k,v in groupby(t, key=lambda d: x<=d<=y):
    if k:
        print(list(v))

Prints:

[datetime.datetime(2040, 2, 2, 0, 0), datetime.datetime(2040, 2, 3, 0, 0), datetime.datetime(2040, 2, 4, 0, 0), datetime.datetime(2040, 2, 5, 0, 0), datetime.datetime(2040, 2, 6, 0, 0)]

On my little laptop that is nearly instant.

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