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 9 5155
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 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
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
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
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
"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
"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
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
"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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nils Grimsmo |
last post by:
Why did round() change in Python 2.4?
$ python2.3
Python 2.3.5 (#2, Jun 19 2005, 13:28:00)
on linux2
>>> round(0.0225, 3)
0.023
>>> "%.3f" % round(0.0225, 3)
'0.023'
>>>
|
by: tmeister |
last post by:
I'm trying to determine the best approach for rounding in an application I'm
building. Unfortunately it appears as though SQL Server and VB.NET round in
different ways.
SQL Server
select...
|
by: Chris Davoli |
last post by:
The folllowing will round to 526, but it should round to 527. It works
correctly for all other numbers, except for this one. Does anybody know of a
bug in Math.Round?
Dim ldecWater As Decimal =...
|
by: Phat G5 (G3) |
last post by:
Has anyone found a reliable way to force JS to round to a specific number of
places? Every time I try I get different results. For example, I'd need to
round 3.4589 to 2 places. What is the most...
|
by: Richard Cranium |
last post by:
Using the number 1.015
*****************************
the following script returns 1.02, which is correct
var T;
var S=new String(Math.round(parseFloat(1.015).toFixed(2)*100));
while...
|
by: David Coleman |
last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2)
0.72
? Math.Round(0.725, 2)
0.72
? Math.Round(0.735, 2)...
|
by: robert |
last post by:
in Gnuplot (Gnuplot.utils) the input array will be converted to a Numeric float array as shown below. When I insert a numpy array into Gnuplot like that below, numbers 7.44 are cast to 7.0
Why is...
|
by: Zeng |
last post by:
Math.Round has good behavior as following:
Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is
even
Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
|
by: lilmax88 |
last post by:
I have a program in which I need to take a numeric value for dollars.
There is a "set" function that must screen the value for the following
3 conditions with the indicated handling functionality:...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |