2012-04-12 92 views
1

我試圖寫一個函數來識別日期組,並測量組的大小。日期比較/連續日期分組

該函數將採用按日期順序排序的元素列表(元素是具有日期的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

回答

1

是這樣的嗎?

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

0

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)