[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

find nearest time in datetime list

washakie

1/30/2008 11:50:00 AM


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-time-in-datetime-list-tp15180398p151...
Sent from the Python - python-list mailing list archive at Nabble.com.

2 Answers

Boris Borcic

1/30/2008 1:45:00 PM

0

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

Tim Chase

1/30/2008 2:06:00 PM

0

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