Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.python
Re: find nearest time in datetime list
washakie
1/30/2008 2:45:00 PM
Thanks all! This is terrific, and a quick response... I have to go with the
2.4 version, but thanks to everyone...
Tim Golden-4 wrote:
>
> washakie wrote:
>> Hello,
>>
>> I have a list of datetime objects: DTlist, I have another single datetime
>> object: dt, ... I need to find the nearest DTlist[i] to the dt .... is
>> there a simple way to do this? There isn't necessarily an exact match...
>
> <code>
> import datetime
>
> dates = [datetime.date (2007, 1, (1+i)*2) for i in range (10)]
> one_date = datetime.date (2007, 1, 7)
>
> print sorted (dates, key=lambda x: abs (x-one_date))[0]
>
> </code>
>
>
> TJG
> --
>
http://mail.python.org/mailman/listinfo/p...
>
>
--
View this message in context:
http://www.nabble.com/find-nearest-time-in-datetime-list-tp15180398p151...
Sent from the Python - python-list mailing list archive at Nabble.com.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: find nearest time in datetime list
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password