Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

find nearest time in datetime list

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

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...

Thanks!
..john


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

 
Reply With Quote
 
 
 
 
Boris Borcic
Guest
Posts: n/a
 
      01-30-2008
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...
>
> Thanks!
> .john
>


min(DTlist,key=lambda date : abs(dt-date))
 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      01-30-2008
Boris Borcic wrote:
> min(DTlist,key=lambda date : abs(dt-date))


In Python2.4:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: min() takes no keyword arguments

Looks like min() only started taking keywords (key) from
Python2.5 forward.

But the min() solution is good if you're running 2.5

-tkc




 
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
Re: find nearest time in datetime list washakie Python 0 01-30-2008 02:44 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