日期比较/连续日期分组

问题描述:

我试图写一个函数来识别日期组,并测量组的大小。日期比较/连续日期分组

该函数将采用按日期顺序排序的元素列表(元素是具有日期的CSV文件中的单独行)。该列表可以是0到n个元素。我希望在输入时填写列表,并添加日期组的大小。

例如,列表

Bill 01/01/2011 

Bill 02/01/2011 

Bill 03/01/2011 

Bill 05/01/2011 

Bill 07/01/2011 

应该输出(理想地打印到文件)作为

Bill 01/01/2011 3 

Bill 02/01/2011 3 

Bill 03/01/2011 3 

Bill 05/01/2011 1 

Bill 07/01/2011 1. 

我有一个函数已经调用isBeside(string1, string2)返回两者之间的增量。

我尝试到目前为止,这是(一个反复的一塌糊涂,我肯定Python可以比这更优雅)

coll[i][1]包含CSV行的日期元素。

def printSet(coll): 
    setSize = len(coll) 
    if setSize == 0: 
    #dont need to do anything 
elif setSize == 1: 

    for i in coll: 
     print i, 1 

elif setSize > 1: 

    printBuffer = [] ##new buffer list which will hold sequential dates, 
         until a non-sequential one is found 
    printBuffer.append(coll[0]) #add the first item 
    print 'Adding ' + str(coll[0]) 

    for i in range(0, len(coll)-1): 

     print 'Comparing ', coll[i][1], coll[i+1][1], isBeside(coll[i][1], coll[i+1][1]) 

     if isBeside(coll[i][1], coll[i+1][1]) == 1: 
      printBuffer.append(coll[i+1]) 
      print 'Adding ' + str(coll[i+1]) 
     else: 
      for j in printBuffer: 
       print j, len(printBuffer) 
      printBuffer = [] 
      printBuffer.append(coll[i]) 

return 
+0

这是一个数据库非常擅长的东西。你有没有考虑过使用数据库呢? – gfortune 2012-04-12 16:30:13

+0

是的。我的问题是我有很多人和CSV文件中的其他变量。程序上似乎是我前进的道路。我最终也需要检查周末/工作日,所以我不认为数据库会阻止,尽管将会被证明是不正确的。我觉得我会用这种方法来关闭,不想把它扔掉:) – Pythonn00b 2012-04-12 16:40:28

+0

日期是月/日/年格式还是日/月/年? – 2012-04-12 16:54:19

是这样的吗?

from datetime import date, timedelta 

coll = [['Bill', date(2011,1,1)], 
     ['Bill', date(2011,1,2)], 
     ['Bill', date(2011,1,3)], 
     ['Bill', date(2011,1,5)], 
     ['Bill', date(2011,1,7)]] 

res = [] 
group = [coll[0]] 
i = 1 

while i < len(coll): 
    row = coll[i] 
    last_in_group = group[-1] 

    # use your isBeside() function here... 
    if row[1] - last_in_group[1] == timedelta(days=1): 
     # consecutive, append to current group.. 
     group.append(row) 
    else: 
     # not consecutive, start new group. 
     res.append(group) 
     group = [row] 
    i += 1 

res.append(group) 

for group in res: 
    for row in group: 
     for item in row: 
      print item, 
     print len(group) 

它打印:

Bill 2011-01-01 3 
Bill 2011-01-02 3 
Bill 2011-01-03 3 
Bill 2011-01-05 1 
Bill 2011-01-07 1 
+0

这是完美的。感谢您将它打印得非常清晰。 – Pythonn00b 2012-04-12 17:26:39

datetime模块是用于同日期,这将是比目前在做你正在使用的字符串比较更清洁的工作非常好。

下面是一个例子:

from datetime import datetime 

def add_month(dt): 
    # Normally you would use timedelta, but timedelta doesn't work with months 
    return dt.replace(year=dt.year + (dt.month==12), month=(dt.month%12) + 1) 

data = ['Bill 01/01/2011', 'Bill 02/01/2011', 'Bill 03/01/2011', 'Bill 05/01/2011', 'Bill 07/01/2011'] 
dates = [datetime.strptime(line.split(' ')[1], '%m/%d/%Y') for line in data] 
buffer = [data[0]] 
for i, date in enumerate(dates[1:]): 
    if add_month(dates[i]) == date: 
     buffer.append(data[i+1]) 
    else: 
     print '\n'.join(line + ' ' + str(len(buffer)) for line in buffer) 
     buffer = [data[i+1]] 

print '\n'.join(line + ' ' + str(len(buffer)) for line in buffer) 

我与你的日期是在形式month/day/year的假设去,如果他们实际上是day/month/year那么你可以添加from datetime import timedelta顶端,在datetime.strptime()格式改为'%d/%m/%y',而不是add_month(dates[i]) == date,请使用date - dates[i] == timedelta(days=1)