find nearest time in datetime list

W

washakie

Hello,

I have a list of datetime objects: DTlist, I have another single datetime
object: dt, ... I need to find the nearest DTlist to the dt .... is
there a simple way to do this? There isn't necessarily an exact match...

Thanks!
..john
 
B

Boris Borcic

washakie said:
Hello,

I have a list of datetime objects: DTlist, I have another single datetime
object: dt, ... I need to find the nearest DTlist 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))
 
T

Tim Chase

Boris said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top