468,785 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

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 16 '06 #1
9 5002
Lance Hoffmeyer <la***@augustmail.com> writes:
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]


[int(x+0.5) for x in Test]

'as
May 16 '06 #2
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:
Lance Hoffmeyer <la***@augustmail.com> writes:
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]


[int(x+0.5) for x in Test]

'as

May 16 '06 #3
On Tue, 16 May 2006 13:41:37 -0500,
Lance Hoffmeyer <la***@augustmail.com> wrote:
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']


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 16 '06 #4

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 16 '06 #5
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

May 16 '06 #6
"Lance Hoffmeyer" <la***@augustmail.com> wrote in message
news:44***********************@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
May 16 '06 #7
"Lance Hoffmeyer" <la***@augustmail.com> wrote in message
news:44***********************@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

.... 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 16 '06 #8
Paul McGuire wrote:
... or if you prefer the functional approach (using map)...

roundToInt = lambda z : int(z+0.5)
Topamax = map( roundToInt, map( float, map(str, Topamax) ) )


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
sc***********@acm.org
May 16 '06 #9
"Paul McGuire" <pt***@austin.rr._bogus_.com> writes:
... 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.)


Your roundToInt behaves differently from round for negative numbers:
roundToInt(-0.6) 0 int(round(-0.6))

-1

Bernhard

--
Intevation GmbH http://intevation.de/
Skencil http://skencil.org/
Thuban http://thuban.intevation.org/
May 17 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Nils Grimsmo | last post: by
10 posts views Thread by tmeister | last post: by
4 posts views Thread by Chris Davoli | last post: by
36 posts views Thread by Phat G5 (G3) | last post: by
1 post views Thread by Richard Cranium | last post: by
10 posts views Thread by David Coleman | last post: by
2 posts views Thread by robert | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.