
May 16th, 2006, 07:25 PM
| | | round numbers in an array without importing Numeric or Math?
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 | 
May 16th, 2006, 07:25 PM
| | | Re: round numbers in an array without importing Numeric or Math?
Lance Hoffmeyer <lance@augustmail.com> writes:
[color=blue]
> 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][/color]
[int(x+0.5) for x in Test]
'as | 
May 16th, 2006, 07:45 PM
| | | Re: round numbers in an array without importing Numeric or Math?
May have a complicating issue with the array? Have
the numbers have been interpreted as strings? I have
been pulling them from a Word doc using regex's
print Test
[u'9.0', u'58.6', u'97.8', u'10.0', u'9.6', u'28.1']
Lance
Alexander Schmolck wrote:[color=blue]
> Lance Hoffmeyer <lance@augustmail.com> writes:
>[color=green]
>> 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][/color]
>
> [int(x+0.5) for x in Test]
>
> 'as[/color] | 
May 16th, 2006, 07:55 PM
| | | Re: round numbers in an array without importing Numeric or Math?
On Tue, 16 May 2006 13:41:37 -0500,
Lance Hoffmeyer <lance@augustmail.com> wrote:
[color=blue]
> May have a complicating issue with the array? Have
> the numbers have been interpreted as strings? I have
> been pulling them from a Word doc using regex's[/color]
[color=blue]
> print Test
> [u'9.0', u'58.6', u'97.8', u'10.0', u'9.6', u'28.1'][/color]
Then you'll have to convert your strings to floats first:
[int(float(x)+0.5) for x in Test]
HTH,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist | 
May 16th, 2006, 07:55 PM
| | | Re: round numbers in an array without importing Numeric or Math?
Lance> May have a complicating issue with the array? Have the numbers
Lance> have been interpreted as strings? I have been pulling them from
Lance> a Word doc using regex's
[int(float(x)+0.5) for x in Test]
S | 
May 16th, 2006, 07:55 PM
| | | Re: round numbers in an array without importing Numeric or Math?- SOLVED, sort of
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:[color=blue]
> 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[/color] | 
May 16th, 2006, 08:25 PM
| | | Re: round numbers in an array without importing Numeric or Math? - SOLVED, sort of
"Lance Hoffmeyer" <lance@augustmail.com> wrote in message
news:446a2032$0$61167$ae4e5890@news.nationwide.net ...[color=blue]
> The array comes out as unicode. This is probably because I am grabbing[/color]
the numbers[color=blue]
> 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:[color=green]
> > 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[/color][/color]
(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 | 
May 16th, 2006, 08:25 PM
| | | Re: round numbers in an array without importing Numeric or Math? - SOLVED, sort of
"Lance Hoffmeyer" <lance@augustmail.com> wrote in message
news:446a2032$0$61167$ae4e5890@news.nationwide.net ...[color=blue]
> The array comes out as unicode. This is probably because I am grabbing[/color]
the numbers[color=blue]
> 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
>
>[/color]
.... or if you prefer the functional approach (using map)...
roundToInt = lambda z : int(z+0.5)
Topamax = map( roundToInt, map( float, map(str, Topamax) ) )
(Python also has a built-in round() function, but this returns floats, not
ints - if that is okay, then just delete the lambda definition, and replace
roundToInt with round.)
-- Paul | 
May 16th, 2006, 11:35 PM
| | | Re: round numbers in an array without importing Numeric or Math?- SOLVED, sort of
Paul McGuire wrote:[color=blue]
> ... or if you prefer the functional approach (using map)...
>
> roundToInt = lambda z : int(z+0.5)
> Topamax = map( roundToInt, map( float, map(str, Topamax) ) )[/color]
Somehow, the list comprehension looks simpler and clearer to me:
Topamax = [int(float(uni) + .5) for uni in Topamax]
I really dislike lines like:
roundToInt = lambda z : int(z+0.5)
You've already chosen a name, but you'd rather write in the lisp style.
def roundToInt(z):return int(z+0.5)
takes all of _one_ more character.
I also don't understand why you convert to string from unicode in:
... map(str, Topamax) ) )
float works on all string types (including unicode), and will accept
some non-ASCII digit values.
--
-Scott David Daniels scott.daniels@acm.org | 
May 17th, 2006, 01:35 PM
| | | Re: round numbers in an array without importing Numeric or Math? -SOLVED, sort of
"Paul McGuire" <ptmcg@austin.rr._bogus_.com> writes:
[color=blue]
> ... or if you prefer the functional approach (using map)...
>
> roundToInt = lambda z : int(z+0.5)
> Topamax = map( roundToInt, map( float, map(str, Topamax) ) )
>
> (Python also has a built-in round() function, but this returns floats, not
> ints - if that is okay, then just delete the lambda definition, and replace
> roundToInt with round.)[/color]
Your roundToInt behaves differently from round for negative numbers:
[color=blue][color=green][color=darkred]
>>> roundToInt(-0.6)[/color][/color][/color]
0[color=blue][color=green][color=darkred]
>>> int(round(-0.6))[/color][/color][/color]
-1
Bernhard
--
Intevation GmbH http://intevation.de/
Skencil http://skencil.org/
Thuban http://thuban.intevation.org/ | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,338 network members.
|