Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75980 > unrolled thread
| Started by | Dave Angel <davea@davea.name> |
|---|---|
| First post | 2014-08-10 07:02 -0400 |
| Last post | 2014-08-10 08:46 -0400 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re:get the min date from a list Dave Angel <davea@davea.name> - 2014-08-10 07:02 -0400
Re: get the min date from a list Roy Smith <roy@panix.com> - 2014-08-10 08:46 -0400
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-08-10 07:02 -0400 |
| Subject | Re:get the min date from a list |
| Message-ID | <mailman.12816.1407668534.18130.python-list@python.org> |
luofeiyu <elearn2014@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
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-08-10 08:46 -0400 |
| Message-ID | <roy-C9CF9E.08465310082014@news.panix.com> |
| In reply to | #75980 |
In article <mailman.12816.1407668534.18130.python-list@python.org>, Dave Angel <davea@davea.name> wrote: > 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) Wow, after all these years, I didn't know min() took a key argument. Of course, it makes sense, but I just never noticed that before. Thanks! And for the OP, for the converter function, I would suggest dateutil.parse.parser(), from the python-dateutil module (https://labix.org/python-dateutil).
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web