将python列表分组为3小时的时间段

时间:2014-02-14 06:30:21

标签: python datetime

我有一个类似的列表:

[{datetime.date(2014, 2, 13): [datetime.time(13, 53, 53), datetime.time(13, 7, 52), datetime.time(12, 43, 35), datetime.time(12, 3, 14), datetime.time(11, 54, 53), datetime.time(10, 52, 42), datetime.time(10, 46, 3), datetime.time(10, 32, 50), datetime.time(10, 32, 38), datetime.time(10, 27, 38), datetime.time(10, 13, 50), datetime.time(10, 9, 59), datetime.time(10, 9, 48), datetime.time(10, 7, 42), datetime.time(10, 7, 20), datetime.time(10, 5, 5)]}

我希望将整个内容插入3小时的插槽中,以便显示为:

datetime.date(2014, 2, 14):

time-slot : 
        datetime.time(0,0,0) - datetime.time(3,0,0)
           .
           . #all times in this slot
        datetime.time(3,0,0) - datetime.time(6,0,0)
           .
           . #all times in this slot

等等..  我不太明白如何去做。任何人都可以通过一些提示或一些算法来帮助我理解如何做到这一点吗?

2 个答案:

答案 0 :(得分:2)

from pprint import pprint
result = {}
for current_dict in d:
    for key in current_dict:
        slots = {}
        for item in current_dict[key]:
            slots.setdefault(item.hour/3+1, [])
            slots[item.hour/3+1].append(item)
        result[key] = slots
pprint(result)

<强>输出

{datetime.date(2014, 2, 13): {4: [datetime.time(11, 54, 53),
                                  datetime.time(10, 52, 42),
                                  datetime.time(10, 46, 3),
                                  datetime.time(10, 32, 50),
                                  datetime.time(10, 32, 38),
                                  datetime.time(10, 27, 38),
                                  datetime.time(10, 13, 50),
                                  datetime.time(10, 9, 59),
                                  datetime.time(10, 9, 48),
                                  datetime.time(10, 7, 42),
                                  datetime.time(10, 7, 20),
                                  datetime.time(10, 5, 5)],
                              5: [datetime.time(13, 53, 53),
                                  datetime.time(13, 7, 52),
                                  datetime.time(12, 43, 35),
                                  datetime.time(12, 3, 14)]}}

答案 1 :(得分:2)

您还可以将groupbyfrom itertools import groupby)与列表理解结合使用:

[(time_slot, [time_entry for time_slot, time_entry in time_entries]) for time_slot, time_entries  in groupby([(timelist_item.hour // 3, timelist_item) for timelist_item in timelist], lambda x: x[0])]

产生以下输出:

[(4,
  [datetime.time(13, 53, 53),
   datetime.time(13, 7, 52),
   datetime.time(12, 43, 35),
   datetime.time(12, 3, 14)]),
 (3,
  [datetime.time(11, 54, 53),
   datetime.time(10, 52, 42),
   datetime.time(10, 46, 3),
   datetime.time(10, 32, 50),
   datetime.time(10, 32, 38),
   datetime.time(10, 27, 38),
   datetime.time(10, 13, 50),
   datetime.time(10, 9, 59),
   datetime.time(10, 9, 48),
   datetime.time(10, 7, 42),
   datetime.time(10, 7, 20),
   datetime.time(10, 5, 5)])]

使用您的数据如下:

timelist = [datetime.time(13, 53, 53), datetime.time(13, 7, 52), datetime.time(12, 43, 35), datetime.time(12, 3, 14), datetime.time(11, 54, 53), datetime.time(10, 52, 42), datetime.time(10, 46, 3), datetime.time(10, 32, 50), datetime.time(10, 32, 38), datetime.time(10, 27, 38), datetime.time(10, 13, 50), datetime.time(10, 9, 59), datetime.time(10, 9, 48), datetime.time(10, 7, 42), datetime.time(10, 7, 20), datetime.time(10, 5, 5)]

timelist是字典日密钥的值。正如@thefourtheye所指出的那样,这个列表必须经过排序才能与groupby一起使用:

dictionary_list = [{datetime.date(2014, 2, 13): [datetime.time(13, 53, 53), ...
timelist = dictionary_list[0].items()[0][1]
timelist.sort()