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 可以比这更优雅)

Notecoll[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
4

2 回答 2

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
于 2012-04-12T16:53:19.373 回答
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)

于 2012-04-12T16:59:50.533 回答