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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <elearn2014@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'rename': 0.07; 'string': 0.09; 'converts': 0.09; 'objects,': 0.09; 'skip:t 60': 0.09; 'python': 0.11; 'def': 0.12; 'assume': 0.14; 'pythonic': 0.16; 'simplest': 0.16; 'skip:> 20': 0.16; 'skip:[ 40': 0.16; 'sorting': 0.16; 'do,': 0.16; 'finished': 0.19; 'import': 0.22; '+0000': 0.22; 'aug': 0.22; 'header:User-Agent:1': 0.23; 'documented': 0.24; 'specify': 0.24; 'string,': 0.24; 'least': 0.26; 'to:2**1': 0.27; 'function': 0.29; 'testing': 0.29; 'skip:p 30': 0.29; 'to:no real name:2**1': 0.29; 'subject:list': 0.30; 'code': 0.31; 'assumes': 0.31; 'probably': 0.32; 'skip:t 40': 0.33; 'subject:the': 0.34; 'subject:from': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'dates': 0.36; 'object,': 0.36; "didn't": 0.36; "i'll": 0.36; 'list': 0.37; 'received:10': 0.37; 'minimum': 0.38; 'message-id:@gmail.com': 0.38; 'version,': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:t 30': 0.61; 'name': 0.63; 'such': 0.63; 'more': 0.64; 'here': 0.66; 'subject:get': 0.81; '2.5.': 0.84; 'min': 0.84; '+0800': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=1yNcFzg+gVsw0KfHOrNryZ2ASLSEtOcKXJRf4dxbU0g=; b=V9TMJUM4zPCzn7/HHd3Z5o58Uf96CIXzrwVowaxm2T9BaLRmAPAeeK0GcGRn0PQHk0 zTwAFVhZAvwT0X33TxTpzXf8Bd/xW5iZDQY065wCudyCrhscSOpbs2Pxyx6HocolCMoe /9NE7fKxfJQ9174q19AE03gX2IMAJ9JB+nBAsbpyl0ooWUnIqGUTrMwBo3kDRVPAJimI /yP6NFGy7At3E7veggvslvH94+JCurAGAFcLivWIH41WHTzWVE3jaa7n40je9Nn0hsrU MpfDa4fgQdGFPsdlXarHVoSSWPCsKmHa4d2ASLhT3A0BuNUImb5fZbosj7CYFVwwZjiO /EFg==
X-Received by 10.70.55.228 with SMTP id v4mr5444664pdp.117.1408025447069; Thu, 14 Aug 2014 07:10:47 -0700 (PDT)
Date Thu, 14 Aug 2014 22:10:36 +0800
From luofeiyu <elearn2014@gmail.com>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0
MIME-Version 1.0
To python-list@python.org, davea@davea.name
Subject Re: get the min date from a list
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.12997.1408025456.18130.python-list@python.org> (permalink)
Lines 108
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1408025456 news.xs4all.nl 2910 [2001:888:2000:d::a6]:41938
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:76307

Show key headers only | 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