列表理解中不变的变量

Unchanging Variable in list comprehension

我有一个这样的列表:

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)]

我正在尝试使用此表达式查找开始日期和结束日期之间缺失的日期:

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

这是一个丑陋的表达式,迫使我用相同的变量填充第二个列表。谁有更简洁的表达方式?

类似于下面的内容。找到最小值和最大值从最小值循环到最大值,看看缺少哪个日期。

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)]

如果您的 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))