Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79935
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2014-10-29 12:09 -0700 |
| References | <406463d0-1287-49a2-b1ed-08a928db4b94@googlegroups.com> <mailman.15295.1414583003.18130.python-list@python.org> |
| Message-ID | <8b20c9ae-ebde-41fd-8569-394e1372fb81@googlegroups.com> (permalink) |
| Subject | Re: Python Style Question |
| From | Anton <anton.schattenfeld@gmail.com> |
On Wednesday, October 29, 2014 4:43:33 AM UTC-7, Rafael Romero Carmona wrote:
> Hi, first in Python 2.7.6 and Python 3.4.0 list haven't got any add
> function but they have append.
You are right, in my original code I use set instead of array, so it should be either values = set() or values.append() in the original code.
>
> I think you could do better with something like
>
> ==========
> import json
> l = [1, -1, 0, '1', '-1', '0', json.dumps(-1), json.dumps(1),
> json.dumps(0), 'x', 'sqjklsqjk__', (1, 2)]
It should also work with cases like [1, json.dumps('-1')], which is case 3), sorry if it was not clear in the initial post.
>
> values = []
>
> for c in l:
> try:
> c_int = int(c)
> except ValueError:
> pass
> except TypeError:
> pass
> else:
> values.append(c_int)
> continue
> print(values)
> ==========
>
> The code has been tested in Python 2.7.6 and 3.4 and returns [1, -1,
> 0, 1, -1, 0, -1, 1, 0]
>
> You don't need to do two try because you can process both exceptions
> in the same way. You don't really need to do json.loads because if you
> have a json string which is an integer, you could do that directly
> with int(c) which can take a string and transform in an integer.
In case of 3) an element can be a string like '"1"', which will fail int(...), in this case it tries to parse it with json.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-10-29 03:42 -0700
Re: Python Style Question Martin Kemp <martin.kemp@ingg.com> - 2014-10-29 11:25 +0000
Re: Python Style Question Rafael Romero Carmona <rafaelromcar@gmail.com> - 2014-10-29 12:09 +0100
Re: Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-10-29 12:09 -0700
Re: Python Style Question Rafael Romero Carmona <rafaelromcar@gmail.com> - 2014-10-29 12:52 +0100
Re: Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-10-29 12:23 -0700
Re: Python Style Question Martin Kemp <martin.kemp@ingg.com> - 2014-10-29 12:42 +0000
Re: Python Style Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-30 22:10 +1100
Re: Python Style Question MRAB <python@mrabarnett.plus.com> - 2014-10-30 12:21 +0000
Re: Python Style Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-31 09:48 +1100
Re: Python Style Question Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-31 04:55 +0000
Re: Python Style Question Roy Smith <roy@panix.com> - 2014-10-30 20:34 -0400
Re: Python Style Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-31 14:52 +1100
Re: Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-11-12 12:31 -0800
csiph-web