Hi,
Great! My own solution was more like Wolfram's. But your solution,
Pierre, really runs under "obfuscating enhanced". I like it. And your
idea, Andrei, is something I really didn't expect.
Anyway, sorry for my late reply. Guess I learned a lot again.
Thanks,
Christian
Pierre Barbier de Reuille schrieb:
Mmmhhh ... not strictly one line but ...
import re
float_str = re.compile(r"^\s*[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?\s*$")
val = [ ( (float_str.match(s) and [float(s)]) or [s])[0] for s in l2 ]
It's not really "readable" but well ... it works ^_^
Pierre
This c
Pierre Barbier de Reuille wrote:
[EMAIL PROTECTED] a écrit :
This is a perfect opportunity to give the reminder that the
conversion functions are also types that can be used more
transparently for such
Neat I didn't know\ that. How dioes Python equate a function object
to a type? Is it hard wired?
[EMAIL PROTECTED] a écrit :
This is a perfect opportunity to give the reminder that the
conversion
functions are also types that can be used more transparently for such
Neat I didn't know\ that. How dioes Python equate a function object
to a type? Is it hard wired?
No, you see it the wrong way !
>This is a perfect opportunity to give the reminder that the
>conversion
>functions are also types that can be used more transparently for such
Neat I didn't know\ that. How dioes Python equate a function object
to a type? Is it hard wired?
> >>> [(type(x)==int and float(x) or x) for x in l]
>[
From: "Alan Gauld" <[EMAIL PROTECTED]>
With other words I'd like to tell Python: Convert into a float if
possible, otherwise append anyway.
[ (type(x) == type(5) and float(x) or x) for x in mylist ]
This is a perfect opportunity to give the reminder that the conversion
functions are also types tha
[(eval(compile('exec """try:t=float("%s")\nexcept:t="%s" """ in
globals(),locals()'%(s,s),'','exec')),t)[1] for s in l]
Now that's ugly.
On Apr 5, 2005 9:51 AM, Alan Gauld <[EMAIL PROTECTED]> wrote:
> > With other words I'd like to tell Python: Convert into a float if
> > possible, otherwise ap
> With other words I'd like to tell Python: Convert into a float if
> possible, otherwise append anyway.
[ (type(x) == type(5) and float(x) or x) for x in mylist ]
Should do it...
Alan G.
___
Tutor maillist - Tutor@python.org
http://mail.python.org
Mmmhhh ... not strictly one line but ...
import re
float_str = re.compile(r"^\s*[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?\s*$")
val = [ ( (float_str.match(s) and [float(s)]) or [s])[0] for s in l2 ]
It's not really "readable" but well ... it works ^_^
Pierre
Christian Meesters a écrit :
Hi
Yesterday
Hi
Yesterday night I was thinking about the following problem:
I do have a list like l = ['1','2','3','abc','','4'] - for instance
like a list one could get from a file import with the csv module (which
is where my 'problem' comes from). Now I would like to generate the
following list, preferabl
10 matches
Mail list logo