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