find nearest time in datetime list

Discussion in 'Python' started by washakie, Jan 30, 2008.

  1. washakie

    washakie Guest

    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


    --
    View this message in context: http://www.nabble.com/find-nearest-time-in-datetime-list-tp15180398p15180398.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
    washakie, Jan 30, 2008
    #1
    1. Advertising

  2. washakie

    Boris Borcic Guest

    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 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))
    Boris Borcic, Jan 30, 2008
    #2
    1. Advertising

  3. washakie

    Tim Chase Guest

    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
    Tim Chase, Jan 30, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    703
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    529
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    386
    John Machin
    Jul 28, 2006
  4. washakie
    Replies:
    0
    Views:
    720
    washakie
    Jan 30, 2008
  5. Martin
    Replies:
    0
    Views:
    333
    Martin
    Dec 27, 2008
Loading...

Share This Page