[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:


for i in range(len(dates_list)): 



    TstartNew=sorted(dates_dt,key=lambda d: abs(Tstart - d))[0] 


    TendNew=sorted(dates_dt, key=lambda d: abs(Tend -d))[0] 



More information about the Tutor mailing list