How do I find missing dates in a list of sorted dates?

前端 未结 8 1311
孤街浪徒
孤街浪徒 2021-02-04 04:15

In Python how do I find all the missing days in a sorted list of dates?

8条回答
  •  孤独总比滥情好
    2021-02-04 04:36

    import datetime
    
    DAY = datetime.timedelta(days=1)
    # missing dates: a list of [start_date, end)
    missing = [(d1+DAY, d2) for d1, d2 in zip(dates, dates[1:]) if (d2 - d1) > DAY]
    
    def date_range(start_date, end, step=DAY):
        d = start_date
        while d < end:
            yield d
            d += step
    
    missing_dates = [d for d1, d2 in missing for d in date_range(d1, d2)]
    

提交回复
热议问题