1

我有一个这样的列表:

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

谢谢

4

2 回答 2

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

如果键尚未按日期排序,则使用 acollections.defaultdict(list)datetime.date()用作键。

于 2013-11-13T21:58:56.287 回答
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))

祝你好运!

于 2013-11-13T22:03:36.857 回答