我有一个这样的列表:
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)]
我希望按天将列表创建到另一个列表中,我使用[n]。日期()来比较日期并加入它们,但是我在没有得到它的情况下运行,请有人帮我吗?
预期结果:
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()
谢谢
答案 0 :(得分:1)
使用itertools.groupby
并使用key = datetime.date()
分组:
.date()
对象返回datetime
对象上的 date
,我们可以使用它来对列表中的项进行分组:
>>> my[0].date()
datetime.date(2013, 11, 12)
<强>演示:强>
>>> from pprint import pprint
>>> from itertools import groupby
>>> pprint([list(g) for k, g in groupby(my, key=lambda x:x.date())])
[[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)]]
如果密钥尚未按日期排序,请使用collections.defaultdict(list)
并使用datetime.date()
作为密钥。
答案 1 :(得分:1)
好的悟空,让我告诉你如何使用itertools.groupby
。这里:
for date, group in itertools.groupby(my, lambda x: x.day):
new_list.append(list(group))
或列表理解:
new_list = [list(group) for day, group in itertools.groupby(my, lambda x: x.day)]
演示:
>>> import itertools
>>> new_list = []
>>> for date, group in itertools.groupby(my, lambda x: x.day):
new_list.append(list(group))
>>> new_list
[[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)]]
>>>
编辑:对不起,我以为你是白天分组的。如果您想按日期对其进行分组,请执行以下操作:
for date, group in itertools.groupby(my, lambda x: x.date()):
new_list.append(list(group))
祝你好运!