我有一个这样的列表:
my = [datetime.datetime(2013, 11, 12, 20, 22, 5),
datetime.datetime(2013, 11, 12, 20, 21, 27),
datetime.datetime(2013, 11, 12, 20, 20, 28),
datetime.datetime(2013, 11, 11, 18, 21, 27),
datetime.datetime(2013, 11, 11, 15, 21, 27),
datetime.datetime(2013, 11, 11, 11, 21, 27),
datetime.datetime(2013, 11, 10, 9, 21, 27),
datetime.datetime(2013, 11, 10, 19, 21, 27),
datetime.datetime(2013, 11, 10, 22, 21, 27),
datetime.datetime(2013, 11, 10, 20, 21, 27)]
我想按天将列表创建到另一个列表中,我正在使用 a[n].date() 来比较日期并加入它们,但是我在没有得到它的情况下绕着圈子跑,有人可以帮我吗?
预期结果:
days = [
[datetime.datetime(2013, 11, 12, 20, 22, 5), datetime.datetime(2013, 11, 12, 20, 21, 27), datetime.datetime(2013, 11, 12, 20, 20, 28)],
[datetime.datetime(2013, 11, 11, 18, 21, 27),datetime.datetime(2013, 11, 11, 15, 21, 27),datetime.datetime(2013, 11, 11, 11, 21, 27)],
[datetime.datetime(2013, 11, 10, 9, 21, 27),datetime.datetime(2013, 11, 10, 19, 21, 27),datetime.datetime(2013, 11, 10, 22, 21, 27),datetime.datetime(2013, 11, 10, 20, 21, 27)]
]
尝试失败:
In [152]: fecha = None
.....: dia = []
.....: days = []
.....: for i in my:
.....: if fecha:
.....: if i.date() == fecha:
.....: dia.append(i)
.....: else:
.....: days.append(dia)
.....: fecha = i.date()
.....: else:
.....: fecha = i.date()
谢谢