round numbers in an array without importing Numeric or Math? - SOLVED, sort of

Paul McGuire ptmcg at austin.rr._bogus_.com
Tue May 16 15:18:30 EDT 2006


"Lance Hoffmeyer" <lance at augustmail.com> wrote in message
news:446a2032$0$61167$ae4e5890 at news.nationwide.net...
> The array comes out as unicode.  This is probably because I am grabbing
the numbers
> from a Word Doc using regex's.
>
> So, before rounding I perform the following:
> # Convert to String
> Topamax = [str(x) for x in Topamax]
> # Convert to floating
> Topamax = [float(x) for x in Topamax]
> # Finally, round the number
> Topamax= [(x+0.5) for x in Topamax]
>
> Is there a shorter way?
>
> Lance
>
>
>
> Lance Hoffmeyer wrote:
> > Is there an easy way to round numbers in an array?
> >
> > I have
> > Test = [1.1,2.2,3.7]
> >
> > and want to round so the values are
> >
> > print Test  [1,2,4]
> >
> >
> > Lance

(c.l.py people don't cotton to top-posting - when in Rome...)

# Convert to String, convert to floating, and finally, round the number all
in one swell foop
Topamax= [int(float(str(x))+0.5) for x in Topamax]

-- Paul





More information about the Python-list mailing list