[英]How to detect if dates are consecutive in Python?
我有一個帶有“日期”字段的訪問表。 它有每個記錄的隨機日期。 我已經構建了一個腳本來將所有記錄附加到列表中,然后設置列表以僅過濾掉唯一值:
dateList = []
# cursor search through each record and append all records in the date
# field to a python list
for row in rows:
dateList.append(row.getValue("DATE_OBSERVATION").strftime('%m-%d-%Y'))
# Filter unique values to a set
newList = list(set(dateList))
這將返回(在我的測試表上):
['07 -06-2010','06 -24-2010','07 -05-2010','06 -25-2010']
既然我有“DATE_OBSERVATION”字段的唯一值,我想檢測是否:
我們歡迎所有的建議! 麥克風
您可以使用datetime對象的.toordinal()
方法將日期對象簡單地轉換為整數,而不是滾動自己的consecutive
函數。 序數日期的最大值和最小值之間的差異比集合的長度多一個:
from datetime import datetime
date_strs = ['07-06-2010', '06-24-2010', '07-05-2010', '06-25-2010']
# date_strs = ['02-29-2012', '02-28-2012', '03-01-2012']
# date_strs = ['01-01-2000']
dates = [datetime.strptime(d, "%m-%d-%Y") for d in date_strs]
date_ints = set([d.toordinal() for d in dates])
if len(date_ints) == 1:
print "unique"
elif max(date_ints) - min(date_ints) == len(date_ints) - 1:
print "consecutive"
else:
print "not consecutive"
另一個版本使用與我的其他答案相同的邏輯。
from datetime import date, timedelta
# Definition 1: 1/1/14, 1/2/14, 1/2/14, 1/3/14 is consider consecutive
# Definition 2: 1/1/14, 1/2/14, 1/2/14, 1/3/14 is consider not consecutive
# datelist = [date(2014, 1, 1), date(2014, 1, 3),
# date(2013, 12, 31), date(2013, 12, 30)]
# datelist = [date(2014, 2, 19), date(2014, 2, 19), date(2014, 2, 20),
# date(2014, 2, 21), date(2014, 2, 22)]
datelist = [date(2014, 2, 19), date(2014, 2, 21),
date(2014, 2, 22), date(2014, 2, 20)]
datelist.sort()
previousdate = datelist[0]
for i in range(1, len(datelist)):
#if (datelist[i] - previousdate).days == 1 or (datelist[i] - previousdate).days == 0: # for Definition 1
if (datelist[i] - previousdate).days == 1: # for Definition 2
previousdate = datelist[i]
else:
previousdate = previousdate + timedelta(days=-1)
if datelist[-1] == previousdate:
print "dates are consecutive"
else:
print "dates are not consecutive"
這是我使用reduce()函數的版本。
from datetime import date, timedelta
def checked(d1, d2):
"""
We assume the date list is sorted.
If d2 & d1 are different by 1, everything up to d2 is consecutive, so d2
can advance to the next reduction.
If d2 & d1 are not different by 1, returning d1 - 1 for the next reduction
will guarantee the result produced by reduce() to be something other than
the last date in the sorted date list.
Definition 1: 1/1/14, 1/2/14, 1/2/14, 1/3/14 is consider consecutive
Definition 2: 1/1/14, 1/2/14, 1/2/14, 1/3/14 is consider not consecutive
"""
#if (d2 - d1).days == 1 or (d2 - d1).days == 0: # for Definition 1
if (d2 - d1).days == 1: # for Definition 2
return d2
else:
return d1 + timedelta(days=-1)
# datelist = [date(2014, 1, 1), date(2014, 1, 3),
# date(2013, 12, 31), date(2013, 12, 30)]
# datelist = [date(2014, 2, 19), date(2014, 2, 19), date(2014, 2, 20),
# date(2014, 2, 21), date(2014, 2, 22)]
datelist = [date(2014, 2, 19), date(2014, 2, 21),
date(2014, 2, 22), date(2014, 2, 20)]
datelist.sort()
if datelist[-1] == reduce(checked, datelist):
print "dates are consecutive"
else:
print "dates are not consecutive"
使用數據庫按升序選擇唯一日期:
如果查詢返回單個日期,那么這是您的第一個案例
否則查明日期是否連續:
import datetime def consecutive(a, b, step=datetime.timedelta(days=1)): return (a + step) == b
代碼布局:
dates = <query database>
if all(consecutive(dates[i], dates[i+1]) for i in xrange(len(dates) - 1)):
if len(dates) == 1: # unique
# 1st case: all records have the same date
else:
# the dates are a range of dates
else:
# non-consecutive dates
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.