round numbers in an array without importing Numeric or Math?

L

Lance Hoffmeyer

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
 
A

Alexander Schmolck

Lance Hoffmeyer said:
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
 
L

Lance Hoffmeyer

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 said:
Lance Hoffmeyer said:
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
 
D

Dan Sommers

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
 
S

skip

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
 
L

Lance Hoffmeyer

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
 
P

Paul McGuire

Lance Hoffmeyer said:
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 said:
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
 
P

Paul McGuire

Lance Hoffmeyer said:
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
 
S

Scott David Daniels

Paul said:
... 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.
 
B

Bernhard Herzog

Paul McGuire said:
... 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:
-1

Bernhard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top