Home > front end >  How to find date range in list?
How to find date range in list?

Time:12-06

In a 1-dimensional list containing dates, to what index can it go through 2-dimensional dates?

data = [[1972,1,1],[1972,6,1],[1973,1,1],
           [1974,1,1],[1975,1,1],[1976,1,1],
           [1977,1,1],[1978,1,1],[1979,1,1],
           [1980,1,1],[1981,6,1],[1982,6,1],
           [1983,6,1],[1985,6,1],[1988,1,1],
           [1990,1,1],[1991,1,1],[1992,6,1],
           [1993,6,1],[1994,6,1],[1996,1,1],
           [1997,6,1],[1999,1,1],[2006,1,1],
           [2009,1,1],[2012,6,1],[2015,6,1],
           [2017,1,1] ] #yyyy-mm-dd format

date = [2015,1,6]#user input example


# n is here number of how many indexes data has.
n = 27

 for i in range(len(data)):
     for j in range(len(data[i])):
         if date[0]<data[i][j]:
             n -= 1

n is 26 here, but since the month([2015,1,6]) is behind the 26th index([2015,6,1]), I need to be able to output 25. Or a more reasonable function should be used. any help would be appreciated. thanks.

CodePudding user response:

Since it looks likes data is sorted, you could consider utilizing a binary search, perhaps via the bisect module:

from bisect import bisect_left


def main() -> None:
    data = [[1972, 1, 1], [1972, 6, 1], [1973, 1, 1],
            [1974, 1, 1], [1975, 1, 1], [1976, 1, 1],
            [1977, 1, 1], [1978, 1, 1], [1979, 1, 1],
            [1980, 1, 1], [1981, 6, 1], [1982, 6, 1],
            [1983, 6, 1], [1985, 6, 1], [1988, 1, 1],
            [1990, 1, 1], [1991, 1, 1], [1992, 6, 1],
            [1993, 6, 1], [1994, 6, 1], [1996, 1, 1],
            [1997, 6, 1], [1999, 1, 1], [2006, 1, 1],
            [2009, 1, 1], [2012, 6, 1], [2015, 6, 1],
            [2017, 1, 1]]
    print(f'{len(data) = }')
    date = [2015, 1, 6]
    print(f'{date = }')
    date_insertion_index = bisect_left(data, date)  # Works because data is sorted.
    print(f'{date_insertion_index = }')


if __name__ == '__main__':
    main()

Output:

len(data) = 28
date = [2015, 1, 6]
date_insertion_index = 26

CodePudding user response:

Set n to len of data

Compare the year, month, and day of the input date with the corresponding values in date

If the date in data is after the input date, decrement n

n = len(data)


for date in data:
    
    if date[0] > date[0] or (date[0] == date[0] and date[1] > date[1]) or (date[0] == date[0] and date[1] == date[1] and date[2] > date[2]):
        
        n -= 1


print(n)

The value of n is now the index of the date immediately before the input date

CodePudding user response:

You can convert the lists to actual date

date = datetime(2015, 1, 6)
n = 27
for l in data:
    if date < datetime(*l):
        n -= 1

print(n) # 25

Or without the variable

date = datetime(2015, 1, 6)
for i, l in enumerate(data):
    if date < datetime(*l):
        break
print(i - 1)

CodePudding user response:

I suggest to use datetime that is intended to compute dates.

from datetime import datetime

data = [datetime(*x) for x in data]  # your data list converted to datetimes
date = datetime(2015, 1, 6)  # user date
for i, dt in enumerate(data):
    if dt >  date:
        break
print(i-1)  # 26
  • Related