(date,day_of_week)元组列表

时间:2013-06-09 19:35:24

标签: python list datetime syntax

我知道我正在尝试做的事情是可能的。我只是无法搞清楚。我希望有更多使用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

正如你所看到的那样,我一周中没有任何东西 - 所以它甚至不是正确的元组表达式。我已经找到并阅读了我认为是the correct documentation的一周中的一天(见date.weekday()),但我似乎无法让它工作。也许是因为我不太了解对象。

另外我不知道如何改变timedelta返回的格式 - 即datetime.datetime(2013,4,25,0,0) - 因为我不需要单词datetime.datetime来在我创建它们之前,先于我的元组。

我欢迎有关如何做我正在尝试做的任何评论或反馈 - 假设它是可能的。提前感谢您的任何建议。

1 个答案:

答案 0 :(得分: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)]]