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


Groups > comp.lang.python > #76307

Re: get the min date from a list

Date 2014-08-14 22:10 +0800
From luofeiyu <elearn2014@gmail.com>
Subject Re: get the min date from a list
Newsgroups comp.lang.python
Message-ID <mailman.12997.1408025456.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

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

csiph-web