Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: find nearest time in datetime list

Reply
Thread Tools

Re: find nearest time in datetime list

 
 
washakie
Guest
Posts: n/a
 
      01-30-2008

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/python-list
>
>


--
View this message in context: http://www.nabble.com/find-nearest-t...p15183205.html
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[2.4.4] creating a datetime.datetime from an XML xs:dateTime Martin Python 0 12-27-2008 08:08 PM
find nearest time in datetime list washakie Python 2 01-30-2008 02:06 PM
mx.DateTime to datetime.datetime mp Python 1 07-28-2006 10:57 PM
datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Christos TZOTZIOY Georgiou Python 3 09-13-2003 10:44 AM
RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Tim Peters Python 0 09-09-2003 12:57 AM



Advertisments