[Tutor] seek and slice a range in a list of dates
Kent Johnson
kent37 at tds.net
Sun Feb 10 02:50:37 CET 2008
washakie wrote:
> It's not pretty, but this is what seems to work... I'd be happy hear more
> about a better approach... I like the idea of using a list, but ran into
> troubles with min and max, maybe I don't entirely understand the lambda
> function, but when I tried a list, I got an error that min() has no optional
> variable 'key' or something along those lines...
You must be using an older version of Python, the key= parameter was
introduced in Python 2.5.
You can still do it more easily with a list using your sort technique:
dates_dt=[]
for i in range(len(dates_list)):
dates_dt.append(datetime.datetime(int(dates_list[i][0][:4]),int(dates_list[i][0][4:6]),\
int(dates_list[i][0][6:8]),int(dates_list[i][0][8:10]),int(dates_list[i][0][10:12]),\
int(dates_list[i][0][12:])))
TstartNew=sorted(dates_dt,key=lambda d: abs(Tstart - d))[0]
Tstart_i=dates_dt.index(TstartNew)
TendNew=sorted(dates_dt, key=lambda d: abs(Tend -d))[0]
Tend_i=dates_dt.index(TendNew)
Kent
More information about the Tutor
mailing list