简体   繁体   中英

Quick way of matching date range to indices in Python list of dates

I need to select the index of the earliest date which is no more than interval days before date1 (with index i1 ). I have a sorted list dates and this is the snippet of what I'm trying to do:

for i1 in mylist:
    date1 = dates[i1]
    i0 = sum(1 for d in dates if date1 - d > timedelta(days = interval))
    # do some other stuff with this

The line where I find i0 seems to be the bottleneck of this loop, because if I change it to i0 = max(0, i1 - 30) (which simply ignores missing dates), it works about 100 times quicker.

Is there a way to speed it up? I feel like there should be a way of using the fact that the list is sorted and avoid doing all the comparisons.


PS: My first try at it was:

i0 = len([d for d in dates if date1 - d > timedelta(days = interval)])

which is even slower.

I need to select the index of the earliest date which is no more than interval days before date1 (with index i1 ). I have a sorted list dates

Using binary search ( O(log n) time complexity):

import bisect

i = bisect.bisect_left(dates, dates[i1]-timedelta(days=interval))

Paraphrasing bisect 's documentation : the return value i is such that all dates in the slice dates[:i] are more than ( > ) interval days before date[i1] , and all dates in the slice dates[i:] are less than or exactly ( <= ) interval days before dates[i1] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM