将在任意时间范围内的任意日期对象组合在一起 [英] Group together arbitrary date objects that are within a time range of each other

查看:159
本文介绍了将在任意时间范围内的任意日期对象组合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将日历分为两个星期,从 2008-May-5 或任意起点开始。

I want to split the calendar into two-week intervals starting at 2008-May-5, or any arbitrary starting point.

所以我从几个日期对象开始:

So I start with several date objects:

import datetime as DT

raw = ("2010-08-01",
       "2010-06-25",
       "2010-07-01",
       "2010-07-08")

transactions = [(DT.datetime.strptime(datestring, "%Y-%m-%d").date(),
                 "Some data here") for datestring in raw]
transactions.sort()

通过手动分析日期,我很清楚日期在相同的两周间隔内。我想得到类似于这样的分组:

By manually analyzing the dates, I am quite able to figure out which dates fall within the same fortnight interval. I want to get grouping that's similar to this one:

# Fortnight interval 1
(datetime.date(2010, 6, 25), 'Some data here')
(datetime.date(2010, 7, 1), 'Some data here')
(datetime.date(2010, 7, 8), 'Some data here')

# Fortnight interval 2
(datetime.date(2010, 8, 1), 'Some data here')


推荐答案

import datetime as DT
import itertools

start_date=DT.date(2008,5,5)

def mkdate(datestring):
    return DT.datetime.strptime(datestring, "%Y-%m-%d").date()

def fortnight(date):
    return (date-start_date).days //14

raw = ("2010-08-01",
       "2010-06-25",
       "2010-07-01",
       "2010-07-08")
transactions=[(date,"Some data") for date in map(mkdate,raw)]
transactions.sort(key=lambda (date,data):date)

for key,grp in itertools.groupby(transactions,key=lambda (date,data):fortnight(date)):
    print(key,list(grp))



< (55,[(datetime.date(2010,6,25),'Some data')]

yields

# (55, [(datetime.date(2010, 6, 25), 'Some data')])
# (56, [(datetime.date(2010, 7, 1), 'Some data'), (datetime.date(2010, 7, 8), 'Some data')])
# (58, [(datetime.date(2010, 8, 1), 'Some data')])


$ b $请注意,2010-6-25是2008-5-5第55周,而2010-7-1是第56。如果您希望将它们分组在一起,只需更改 start_date (如2008-5-16)。

Note that 2010-6-25 is in the 55th fortnight from 2008-5-5, while 2010-7-1 is in the 56th. If you want them grouped together, simply change start_date (to something like 2008-5-16).

PS 。上面使用的关键工具是 itertools.groupby ,详细说明 here

PS. The key tool used above is itertools.groupby, which is explained in detail here.

编辑: lambda 匿名功能的方法。 (他们是匿名的,因为他们没有给出名称,例如由 def 定义的功能)。任何地方你看到一个lambda,也可以使用 def 创建一个等效的功能。例如,您可以这样做:

The lambdas are simply a way to make "anonymous" functions. (They are anonymous in the sense that they are not given names like functions defined by def). Anywhere you see a lambda, it is also possible to use a def to create an equivalent function. For example, you could do this:

import operator
transactions.sort(key=operator.itemgetter(0))

def transaction_fortnight(transaction):
    date,data=transaction
    return fortnight(date)

for key,grp in itertools.groupby(transactions,key=transaction_fortnight):
    print(key,list(grp))

这篇关于将在任意时间范围内的任意日期对象组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆