Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76307 > unrolled thread
| Started by | luofeiyu <elearn2014@gmail.com> |
|---|---|
| First post | 2014-08-14 22:10 +0800 |
| Last post | 2014-08-15 15:13 -0600 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
Re: get the min date from a list luofeiyu <elearn2014@gmail.com> - 2014-08-14 22:10 +0800
Re: get the min date from a list Marko Rauhamaa <marko@pacujo.net> - 2014-08-14 20:19 +0300
Re: get the min date from a list Denis McMahon <denismfmcmahon@gmail.com> - 2014-08-15 18:21 +0000
Re: get the min date from a list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-15 21:31 +0100
Re: get the min date from a list Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-15 15:13 -0600
| From | luofeiyu <elearn2014@gmail.com> |
|---|---|
| Date | 2014-08-14 22:10 +0800 |
| Subject | Re: get the min date from a list |
| Message-ID | <mailman.12997.1408025456.18130.python-list@python.org> |
I finished it ,but how to make it into more pythonic way such as
min (dates, key = converter)
here is my code
times=['Sat, 09 Aug 2014 07:36:46 -0700',
'Fri, 8 Aug 2014 22:25:40 -0400',
'Sat, 9 Aug 2014 12:46:43 +1000',
'Sat, 9 Aug 2014 12:50:52 +1000',
'Sat, 9 Aug 2014 02:51:01 +0000 (UTC)',
'Sat, 9 Aug 2014 13:03:24 +1000',
'Sat, 09 Aug 2014 13:06:28 +1000',
'Fri, 8 Aug 2014 20:48:44 -0700 (PDT)',
'Fri, 8 Aug 2014 23:52:09 -0700 (PDT)',
'Sat, 09 Aug 2014 09:15:50 +0200',
'Sat, 9 Aug 2014 01:49:54 -0600',
'Sat, 9 Aug 2014 01:57:18 -0600',
'Sat, 9 Aug 2014 17:54:23 +0800 (CST)',
'Sat, 9 Aug 2014 12:49:08 +0200',
'Sat, 9 Aug 2014 07:31:09 -0400',
'Sat, 9 Aug 2014 07:34:16 -0400',
'Sat, 09 Aug 2014 11:39:16 +0000',
'Sat, 9 Aug 2014 07:40:41 -0400',
'Sat, 9 Aug 2014 11:46:54 +0000',
'Sat, 09 Aug 2014 13:48:17 +0200',
'Sat, 09 Aug 2014 21:53:11 +1000',
'Sat, 09 Aug 2014 14:13:13 +0200',
'Sat, 09 Aug 2014 08:16:08 -0400',
'Sat, 09 Aug 2014 22:17:25 +1000',
'Sat, 09 Aug 2014 14:33:54 +0200',
'Sat, 9 Aug 2014 14:46:01 +0200',
'Sat, 09 Aug 2014 10:34:58 -0300',
'Sat, 09 Aug 2014 11:34:22 -0400',
'Sat, 09 Aug 2014 12:16:56 -0400',
'Sat, 09 Aug 2014 12:26:38 -0400',
'Sat, 09 Aug 2014 13:29:59 -0400',
'Sat, 09 Aug 2014 13:43:33 -0400',
'Sat, 9 Aug 2014 11:30:35 -0300',
'Sat, 09 Aug 2014 20:14:20 +0200',
'Sun, 10 Aug 2014 08:18:34 +1000',
'Sat, 9 Aug 2014 18:23:08 -0400 (EDT)',
'Sat, 09 Aug 2014 18:30:17 -0400',
'Sat, 09 Aug 2014 18:31:38 -0400',
'Sun, 10 Aug 2014 09:43:44 +1000',
'Sat, 9 Aug 2014 18:27:15 -0700 (PDT)',
'Sun, 10 Aug 2014 03:44: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 +0000 (UTC)']
def changeToUnix(times):
import time,calendar,re
time_list=[]
for time1 in times:
pat='(.+?)([-|+]\d{4})(\(?.*\)?)'
x=re.search(pat,time1)
time_part=x.groups()[0].strip()
tz_part=x.groups()[1]
tz_acc=x.groups()[2].strip().replace('(','').replace(')','')
num=int(tz_part[1:3])
if tz_acc in ["","UTC","CST","GMT","EST","CST","PST"]: num=num
if tz_acc in ["EDT"]: num=num+2
if tz_acc in ["CDT"]: num=num+1
if tz_acc in ["PDT"]: num=num-1
op=tz_part[0]
y=time.strptime(time_part,"%a, %d %b %Y %H:%M:%S")
if op=="-": hour=int(y.tm_hour)-num
if op=="+": hour=int(y.tm_hour)+num
time2=(y.tm_year,y.tm_mon,y.tm_mday,hour,y.tm_min,y.tm_sec)
time_list.append(calendar.timegm(time2))
return(time_list)
>>>y=changeToUnix(times)
>>>times[y.index(min(y))]
'Tue, 5 Aug 2014 01:55:24 +0000 (UTC)'
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
[toc] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2014-08-14 20:19 +0300 |
| Message-ID | <87r40jaszt.fsf@elektro.pacujo.net> |
| In reply to | #76307 |
luofeiyu <elearn2014@gmail.com>: > y=time.strptime(time_part,"%a, %d %b %Y %H:%M:%S") As I said, whether that works depends on your locale -- according to the reference documentation. In practice, I couldn't get that to fail in my tests. I would be on my guard, though. That might mean I couldn't use strptime to convert the dates. Marko
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-08-15 18:21 +0000 |
| Message-ID | <lslj31$5hh$2@dont-email.me> |
| In reply to | #76307 |
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@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-08-15 21:31 +0100 |
| Message-ID | <mailman.13040.1408134711.18130.python-list@python.org> |
| In reply to | #76381 |
On 15/08/2014 19:21, Denis McMahon wrote: > 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. > I say old bean do be careful, I've been suffering nightmares having been accused of nagging, I wouldn't want you to suffer the same fate. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-08-15 15:13 -0600 |
| Message-ID | <mailman.13041.1408137277.18130.python-list@python.org> |
| In reply to | #76381 |
On Fri, Aug 15, 2014 at 12:21 PM, Denis McMahon
<denismfmcmahon@gmail.com> wrote:
> 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.
>>> datetime.strptime("Mon, 9\t\t\tAug\n2014\r7:36:46\f-0700", "%a, %d %b %Y %H:%M:%S %z")
datetime.datetime(2014, 8, 9, 7, 36, 46,
tzinfo=datetime.timezone(datetime.timedelta(-1, 61200)))
strptime doesn't seem to care about variations in whitespace as long
as some is present, or missing leading zeroes (although it does throw
an error if the time zone offset is only 3 digits).
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web