get the min date from a list
Denis McMahon
denismfmcmahon at gmail.com
Fri Aug 15 14:21:22 EDT 2014
On Thu, 14 Aug 2014 22:10:36 +0800, luofeiyu wrote:
> I finished it ,but how to make it into more pythonic way such as min
> (dates, key = converter)
1. If you don't learn to post properly, I'm going to stop trying to help
you.
2. To user strptime, you need to have all the time strings in the same
format. Your time strings are not all in the same format.
3. Consider the following code which works on python 3.2:
#!/usr/bin/python3
from datetime import tzinfo, timedelta, datetime, timezone
times=[
'Sat, 09 Aug 2014 07:36:46 -0700',
# rest of array here
'Tue, 05 Aug 2014 01:55:24 +0000',
]
realtimes = [ datetime.strptime( x, "%a, %d %b %Y %H:%M:%S %z" )
for x in times ]
realtimes.sort()
utctimes = [ x.astimezone(timezone(timedelta(0)))
for x in realtimes ]
for i in range( len( realtimes ) ):
print( realtimes[i], "==", utctimes[i] )
Output is a sorted list of the actual times and the UTC equivalents of
all the times in the original list. Note that I had to edit several
strings in your times list to ensure they were all in identical format: I
added leading 0s to numeric values in some strings, deleted extra spaces
in some strings, deleted extraneous information after the tz offset in
some strings. When feeding strings to a parsing function such as strptime
() it is critically important that the format specifier matches the input
data.
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list