Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75980

Re:get the min date from a list

From Dave Angel <davea@davea.name>
Subject Re:get the min date from a list
Date 2014-08-10 07:02 -0400
Organization news.gmane.org
References <53E71BCB.7060307@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.12816.1407668534.18130.python-list@python.org> (permalink)

Show all headers | View raw


luofeiyu <elearn2014@gmail.com> Wrote in message:
>  >>> date
> ['Sat, 09 Aug 2014 07:36:46 -0700', 'Fri, 8 Aug 2014 22:25:40 -0400', 
> 'Sat, 9 Au
> g 2014 12:46:43 +1000', 'Sat, 9 Aug 2014 12:50:52 +1000', 'Sat, 9 Aug 
> ......... 
> 2014 03:4
> 4:56 +0200', 'Sun, 10 Aug 2014 01:55:24 +0000 (UTC)', 'Sun, 10 Aug 2014 
> 02:01:06
>   +0000 (UTC)', 'Sat, 9 Aug 2014 19:41:08 -0700 (PDT)', 'Sat, 9 Aug 2014 
> 22:51:29
>   -0400 (EDT)', 'Sun, 10 Aug 2014 07:34:44 +0200', 'Tue, 5 Aug 2014 
> 01:55:24 +000
> 0 (UTC)']
>  >>> min(date)
> 'Fri, 8 Aug 2014 20:48:44 -0700 (PDT)'
> 
> The result is wrong,the min date should be 'Tue, 5 Aug 2014 01:55:24 +000
> 0 (UTC)' ,how can i get it ?
> 

You neglected to specify your Python version,  but I'll assume at
 least 2.5.

The min function did exactly as it's documented to do, found the
 minimum string, by alphabetical ordering. Since 'F' is less than
 'T' it didn't need to look at the rest.  If you had been sorting
 a list of datetime objects, it would have found the least of
 those. 

Your simplest answer is probably to write a function that converts
 a string like you have into a datetime object, say call it
 converter (). Then after testing it, you call

min (dates, key = converter)

Note I did NOT use parens on converter.

I also used the name dates for the list,  since it's a collection
 of dates. But that assumes you rename it in your code that
 gathered them.

-- 
DaveA

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re:get the min date from a list Dave Angel <davea@davea.name> - 2014-08-10 07:02 -0400
  Re: get the min date from a list Roy Smith <roy@panix.com> - 2014-08-10 08:46 -0400

csiph-web