get the min date from a list
Dave Angel
davea at davea.name
Sun Aug 10 07:02:49 EDT 2014
luofeiyu <elearn2014 at 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
More information about the Python-list
mailing list