0

我有一个这样的列表:

dates = [
    datetime.date(2014, 11, 24),
    datetime.date(2014, 11, 25),
    datetime.date(2014, 11, 26),
    # datetime.date(2014, 11, 27), # This one is missing
    datetime.date(2014, 11, 28),
    datetime.date(2014, 11, 29),
    datetime.date(2014, 11, 30),
    datetime.date(2014, 12, 1)]

我正在尝试使用以下 expr 查找开始日期和结束日期之间缺少的日期:

date_set = {dates[0] + timedelta(x) for x in range((dates[-1] - dates[0]).days)}

奇怪的是,它抛出了一个错误——它不能访问dates变量。但是这个表达式运行良好:

date_set = {date(2015,2,11) + timedelta(x) for x in range((dates[-1] - dates[0]).days)}

我写了一个表达我想要的东西:

def find_missing_dates(dates: list[date]) -> list[date]:
    """Find the missing dates in a list of dates (that should already be sorted)."""
    date_set = {(first_date + timedelta(x)) for first_date, x in zip([dates[0]] * len(dates), range((dates[-1] - dates[0]).days))}
    missing = sorted(date_set - set(dates))

    return missing

这是一个丑陋的表达式,迫使我用相同的变量填充第二个列表。有人有更干净的表达吗?

4

2 回答 2

2

如果您dates已排序,您只需对其进行迭代并将其添加到新列表中。我已经在评论中提供了可能的单线解决方案。

from datetime import date, timedelta

dates = [
    date(2014, 11, 24), date(2014, 11, 25), date(2014, 11, 26),
    date(2014, 11, 28), date(2014, 11, 29), date(2014, 11, 30),
    date(2014, 12, 1)
]
missing = [d + timedelta(days=j) for i, d in enumerate(dates[:-1], 1) for j in range(1, (dates[i] - d).days)]

您可以使用常规 for 循环来做到这一点:

from datetime import date, timedelta

dates = [
    date(2014, 11, 24), date(2014, 11, 25), date(2014, 11, 26),
    date(2014, 11, 28), date(2014, 11, 29), date(2014, 11, 30),
    date(2014, 12, 1)
]

missing = []
for next_index, current_date in enumerate(dates[:-1], 1):
    for days_diff in range(1, (dates[next_index] - current_date).days):
        missing.append(current_date + timedelta(days=days_diff))
于 2021-10-10T18:34:26.667 回答
1

像下面这样的东西。找到最小值和最大值。从 min 循环到 max 并查看缺少哪个日期。

from datetime import timedelta, date

dates = [
    date(2014, 11, 21),
    date(2014, 11, 24),
    date(2014, 11, 25),
    date(2014, 11, 26),
    date(2014, 11, 27),
    date(2014, 11, 28),
    date(2014, 11, 29),
    date(2014, 11, 30),
    date(2014, 12, 1)
]
_min = min(dates)
_max = max(dates)
missing = []
while _min < _max:
    if _min not in dates:
        missing.append(_min)
    _min += timedelta(days=1)
print(missing)

输出

[datetime.date(2014, 11, 22), datetime.date(2014, 11, 23)]
于 2021-10-10T18:29:03.823 回答