Re: find nearest time in datetime list

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

  1. washakie

    washakie Guest

    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 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-time-in-datetime-list-tp15180398p15183205.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
     
    washakie, Jan 30, 2008
    #1
    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:
    745
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    581
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    419
    John Machin
    Jul 28, 2006
  4. washakie

    find nearest time in datetime list

    washakie, Jan 30, 2008, in forum: Python
    Replies:
    2
    Views:
    327
    Tim Chase
    Jan 30, 2008
  5. Martin
    Replies:
    0
    Views:
    364
    Martin
    Dec 27, 2008
Loading...

Share This Page