[Tutor] one line code

Liam Clarke cyresse at gmail.com
Tue Apr 5 00:40:25 CEST 2005


[(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 <alan.gauld at freenet.co.uk> wrote:
> > 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 at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list