1

我知道我正在尝试做的事情是可能的。我只是无法弄清楚。我希望有更多在 python 中处理日期的经验的人可以帮助我。

我正在尝试创建一个日期范围的列表,其中列表中的每个元素都包含一个日期以及该日期代表一周中的哪一天。这是我到目前为止所拥有的。

from datetime import datetime
date_format = "%b %d, %Y"
a = datetime.strptime('Feb 16, 2013', date_format)
b = datetime.strptime('Aug 11, 2013', date_format)
delta = b - a
delta = delta.days # that's it

print a
print b

base = a    

dateList = [ base + timedelta(days=x) for x in range(0,delta) ]

print dateList

正如您所看到的,我在一周中的某一天没有任何内容——所以它甚至不是正确的元组形式的表达式。我已经找到并阅读了我认为是星期几的正确文档(请参阅 date.weekday()),但我似乎无法让它工作。也许是因为我对物体的理解不够好。

此外,我不知道如何更改 timedelta 返回的格式——即 datetime.datetime(2013, 4, 25, 0, 0)——因为我不需要 datetime.datetime 这个词在我的元组之前一旦我创建它们。

我欢迎任何关于如何做我正在尝试做的事情的评论或反馈——假设这是可能的。提前感谢您的任何建议。

4

1 回答 1

2

datetime对象本身有一个.weekday()属性。您可以在单独的循环中添加这些:

dateList = [(d, d.weekday()) for d in dateList]

对于您的示例代码,这给出了:

[(datetime.datetime(2013, 2, 16, 0, 0), 5), (datetime.datetime(2013, 2, 17, 0, 0), 6), (datetime.datetime(2013, 2, 18, 0, 0), 0), (datetime.datetime(2013, 2, 19, 0, 0), 1), (datetime.datetime(2013, 2, 20, 0, 0), 2), (datetime.datetime(2013, 2, 21, 0, 0), 3), (datetime.datetime(2013, 2, 22, 0, 0), 4), (datetime.datetime(2013, 2, 23, 0, 0), 5), (datetime.datetime(2013, 2, 24, 0, 0), 6), (datetime.datetime(2013, 2, 25, 0, 0), 0), (datetime.datetime(2013, 2, 26, 0, 0), 1), (datetime.datetime(2013, 2, 27, 0, 0), 2), (datetime.datetime(2013, 2, 28, 0, 0), 3), (datetime.datetime(2013, 3, 1, 0, 0), 4), (datetime.datetime(2013, 3, 2, 0, 0), 5), (datetime.datetime(2013, 3, 3, 0, 0), 6), (datetime.datetime(2013, 3, 4, 0, 0), 0), (datetime.datetime(2013, 3, 5, 0, 0), 1), (datetime.datetime(2013, 3, 6, 0, 0), 2), (datetime.datetime(2013, 3, 7, 0, 0), 3), (datetime.datetime(2013, 3, 8, 0, 0), 4), (datetime.datetime(2013, 3, 9, 0, 0), 5), (datetime.datetime(2013, 3, 10, 0, 0), 6), (datetime.datetime(2013, 3, 11, 0, 0), 0), (datetime.datetime(2013, 3, 12, 0, 0), 1), (datetime.datetime(2013, 3, 13, 0, 0), 2), (datetime.datetime(2013, 3, 14, 0, 0), 3), (datetime.datetime(2013, 3, 15, 0, 0), 4), (datetime.datetime(2013, 3, 16, 0, 0), 5), (datetime.datetime(2013, 3, 17, 0, 0), 6), (datetime.datetime(2013, 3, 18, 0, 0), 0), (datetime.datetime(2013, 3, 19, 0, 0), 1), (datetime.datetime(2013, 3, 20, 0, 0), 2), (datetime.datetime(2013, 3, 21, 0, 0), 3), (datetime.datetime(2013, 3, 22, 0, 0), 4), (datetime.datetime(2013, 3, 23, 0, 0), 5), (datetime.datetime(2013, 3, 24, 0, 0), 6), (datetime.datetime(2013, 3, 25, 0, 0), 0), (datetime.datetime(2013, 3, 26, 0, 0), 1), (datetime.datetime(2013, 3, 27, 0, 0), 2), (datetime.datetime(2013, 3, 28, 0, 0), 3), (datetime.datetime(2013, 3, 29, 0, 0), 4), (datetime.datetime(2013, 3, 30, 0, 0), 5), (datetime.datetime(2013, 3, 31, 0, 0), 6), (datetime.datetime(2013, 4, 1, 0, 0), 0), (datetime.datetime(2013, 4, 2, 0, 0), 1), (datetime.datetime(2013, 4, 3, 0, 0), 2), (datetime.datetime(2013, 4, 4, 0, 0), 3), (datetime.datetime(2013, 4, 5, 0, 0), 4), (datetime.datetime(2013, 4, 6, 0, 0), 5), (datetime.datetime(2013, 4, 7, 0, 0), 6), (datetime.datetime(2013, 4, 8, 0, 0), 0), (datetime.datetime(2013, 4, 9, 0, 0), 1), (datetime.datetime(2013, 4, 10, 0, 0), 2), (datetime.datetime(2013, 4, 11, 0, 0), 3), (datetime.datetime(2013, 4, 12, 0, 0), 4), (datetime.datetime(2013, 4, 13, 0, 0), 5), (datetime.datetime(2013, 4, 14, 0, 0), 6), (datetime.datetime(2013, 4, 15, 0, 0), 0), (datetime.datetime(2013, 4, 16, 0, 0), 1), (datetime.datetime(2013, 4, 17, 0, 0), 2), (datetime.datetime(2013, 4, 18, 0, 0), 3), (datetime.datetime(2013, 4, 19, 0, 0), 4), (datetime.datetime(2013, 4, 20, 0, 0), 5), (datetime.datetime(2013, 4, 21, 0, 0), 6), (datetime.datetime(2013, 4, 22, 0, 0), 0), (datetime.datetime(2013, 4, 23, 0, 0), 1), (datetime.datetime(2013, 4, 24, 0, 0), 2), (datetime.datetime(2013, 4, 25, 0, 0), 3), (datetime.datetime(2013, 4, 26, 0, 0), 4), (datetime.datetime(2013, 4, 27, 0, 0), 5), (datetime.datetime(2013, 4, 28, 0, 0), 6), (datetime.datetime(2013, 4, 29, 0, 0), 0), (datetime.datetime(2013, 4, 30, 0, 0), 1), (datetime.datetime(2013, 5, 1, 0, 0), 2), (datetime.datetime(2013, 5, 2, 0, 0), 3), (datetime.datetime(2013, 5, 3, 0, 0), 4), (datetime.datetime(2013, 5, 4, 0, 0), 5), (datetime.datetime(2013, 5, 5, 0, 0), 6), (datetime.datetime(2013, 5, 6, 0, 0), 0), (datetime.datetime(2013, 5, 7, 0, 0), 1), (datetime.datetime(2013, 5, 8, 0, 0), 2), (datetime.datetime(2013, 5, 9, 0, 0), 3), (datetime.datetime(2013, 5, 10, 0, 0), 4), (datetime.datetime(2013, 5, 11, 0, 0), 5), (datetime.datetime(2013, 5, 12, 0, 0), 6), (datetime.datetime(2013, 5, 13, 0, 0), 0), (datetime.datetime(2013, 5, 14, 0, 0), 1), (datetime.datetime(2013, 5, 15, 0, 0), 2), (datetime.datetime(2013, 5, 16, 0, 0), 3), (datetime.datetime(2013, 5, 17, 0, 0), 4), (datetime.datetime(2013, 5, 18, 0, 0), 5), (datetime.datetime(2013, 5, 19, 0, 0), 6), (datetime.datetime(2013, 5, 20, 0, 0), 0), (datetime.datetime(2013, 5, 21, 0, 0), 1), (datetime.datetime(2013, 5, 22, 0, 0), 2), (datetime.datetime(2013, 5, 23, 0, 0), 3), (datetime.datetime(2013, 5, 24, 0, 0), 4), (datetime.datetime(2013, 5, 25, 0, 0), 5), (datetime.datetime(2013, 5, 26, 0, 0), 6), (datetime.datetime(2013, 5, 27, 0, 0), 0), (datetime.datetime(2013, 5, 28, 0, 0), 1), (datetime.datetime(2013, 5, 29, 0, 0), 2), (datetime.datetime(2013, 5, 30, 0, 0), 3), (datetime.datetime(2013, 5, 31, 0, 0), 4), (datetime.datetime(2013, 6, 1, 0, 0), 5), (datetime.datetime(2013, 6, 2, 0, 0), 6), (datetime.datetime(2013, 6, 3, 0, 0), 0), (datetime.datetime(2013, 6, 4, 0, 0), 1), (datetime.datetime(2013, 6, 5, 0, 0), 2), (datetime.datetime(2013, 6, 6, 0, 0), 3), (datetime.datetime(2013, 6, 7, 0, 0), 4), (datetime.datetime(2013, 6, 8, 0, 0), 5), (datetime.datetime(2013, 6, 9, 0, 0), 6), (datetime.datetime(2013, 6, 10, 0, 0), 0), (datetime.datetime(2013, 6, 11, 0, 0), 1), (datetime.datetime(2013, 6, 12, 0, 0), 2), (datetime.datetime(2013, 6, 13, 0, 0), 3), (datetime.datetime(2013, 6, 14, 0, 0), 4), (datetime.datetime(2013, 6, 15, 0, 0), 5), (datetime.datetime(2013, 6, 16, 0, 0), 6), (datetime.datetime(2013, 6, 17, 0, 0), 0), (datetime.datetime(2013, 6, 18, 0, 0), 1), (datetime.datetime(2013, 6, 19, 0, 0), 2), (datetime.datetime(2013, 6, 20, 0, 0), 3), (datetime.datetime(2013, 6, 21, 0, 0), 4), (datetime.datetime(2013, 6, 22, 0, 0), 5), (datetime.datetime(2013, 6, 23, 0, 0), 6), (datetime.datetime(2013, 6, 24, 0, 0), 0), (datetime.datetime(2013, 6, 25, 0, 0), 1), (datetime.datetime(2013, 6, 26, 0, 0), 2), (datetime.datetime(2013, 6, 27, 0, 0), 3), (datetime.datetime(2013, 6, 28, 0, 0), 4), (datetime.datetime(2013, 6, 29, 0, 0), 5), (datetime.datetime(2013, 6, 30, 0, 0), 6), (datetime.datetime(2013, 7, 1, 0, 0), 0), (datetime.datetime(2013, 7, 2, 0, 0), 1), (datetime.datetime(2013, 7, 3, 0, 0), 2), (datetime.datetime(2013, 7, 4, 0, 0), 3), (datetime.datetime(2013, 7, 5, 0, 0), 4), (datetime.datetime(2013, 7, 6, 0, 0), 5), (datetime.datetime(2013, 7, 7, 0, 0), 6), (datetime.datetime(2013, 7, 8, 0, 0), 0), (datetime.datetime(2013, 7, 9, 0, 0), 1), (datetime.datetime(2013, 7, 10, 0, 0), 2), (datetime.datetime(2013, 7, 11, 0, 0), 3), (datetime.datetime(2013, 7, 12, 0, 0), 4), (datetime.datetime(2013, 7, 13, 0, 0), 5), (datetime.datetime(2013, 7, 14, 0, 0), 6), (datetime.datetime(2013, 7, 15, 0, 0), 0), (datetime.datetime(2013, 7, 16, 0, 0), 1), (datetime.datetime(2013, 7, 17, 0, 0), 2), (datetime.datetime(2013, 7, 18, 0, 0), 3), (datetime.datetime(2013, 7, 19, 0, 0), 4), (datetime.datetime(2013, 7, 20, 0, 0), 5), (datetime.datetime(2013, 7, 21, 0, 0), 6), (datetime.datetime(2013, 7, 22, 0, 0), 0), (datetime.datetime(2013, 7, 23, 0, 0), 1), (datetime.datetime(2013, 7, 24, 0, 0), 2), (datetime.datetime(2013, 7, 25, 0, 0), 3), (datetime.datetime(2013, 7, 26, 0, 0), 4), (datetime.datetime(2013, 7, 27, 0, 0), 5), (datetime.datetime(2013, 7, 28, 0, 0), 6), (datetime.datetime(2013, 7, 29, 0, 0), 0), (datetime.datetime(2013, 7, 30, 0, 0), 1), (datetime.datetime(2013, 7, 31, 0, 0), 2), (datetime.datetime(2013, 8, 1, 0, 0), 3), (datetime.datetime(2013, 8, 2, 0, 0), 4), (datetime.datetime(2013, 8, 3, 0, 0), 5), (datetime.datetime(2013, 8, 4, 0, 0), 6), (datetime.datetime(2013, 8, 5, 0, 0), 0), (datetime.datetime(2013, 8, 6, 0, 0), 1), (datetime.datetime(2013, 8, 7, 0, 0), 2), (datetime.datetime(2013, 8, 8, 0, 0), 3), (datetime.datetime(2013, 8, 9, 0, 0), 4), (datetime.datetime(2013, 8, 10, 0, 0), 5)]

您可以通过使用一个元素创建一个额外的嵌套循环,将其与现有的列表理解结合起来:

dateList = [(d, d.weekday()) for x in range(0,delta) for d in [base + timedelta(days=x)]]
于 2013-06-09T19:42:24.967 回答