Reputation: 383
I have a list of numbers which I need to round into integers before I continue using the list. Example source list:
[25.0, 193.0, 281.75, 87.5, 80.5, 449.75, 306.25, 281.75, 87.5, 675.5,986.125, 306.25, 281.75]
What would I do to save this list with all of the numbers rounded to an integer?
Upvotes: 16
Views: 51479
Reputation: 2897
If your original list of floats is in a numpy array you can use numpy.rint(). Somewhat annoyingly, although it rounds floating point values to the nearest integer, it still leaves them as floats, so you also have to cast them to ints afterwards. Example:
import numpy as np
x = np.array([2.0, -1.6, -1.2, 0.5, 1.5, 2.5, 2.45, 2.55, 1.8])
rounded = np.rint(x).astype(int)
# If you want to convert the array back into a regular Python list:
print(rounded.tolist()) # [2, -2, -1, 0, 2, 2, 2, 3, 2]
Upvotes: 0
Reputation: 251
If you would set the number of significant digits you could do
new_list = list(map(lambda x: round(x,precision),old_list))
Furthermore, if you had a list of list you could do
new_list = [list(map(lambda x: round(x,precision),old_l)) for old_l in old_list]
Upvotes: 4
Reputation: 20336
You could use the built-in function round()
with a list comprehension:
newlist = [round(x) for x in list]
You could also use the built-in function map()
:
newlist = list(map(round, list))
I wouldn't recommend list
as a name, though, because you are shadowing the built-in type.
Upvotes: 13
Reputation: 13106
Updating this for python3 since other answers leverage python2's map
, which returns a list
, where python3's map
returns an iterator. You can have the list
function consume your map
object:
l = [25.0, 193.0, 281.75, 87.5, 80.5, 449.75, 306.25, 281.75, 87.5, 675.5,986.125, 306.25, 281.75]
list(map(round, l))
[25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
To use round
in this way for a specific n
, you'll want to use functools.partial
:
from functools import partial
n = 3
n_round = partial(round, ndigits=3)
n_round(123.4678)
123.468
new_list = list(map(n_round, list_of_floats))
Upvotes: 1
Reputation: 702
NumPy is great for handling arrays like this.
Simply np.around(list)
or np.round(list)
works.
Upvotes: 2
Reputation: 6575
Another approach using map
function.
You can set how many digits to round
.
>>> floats = [25.0, 193.0, 281.75, 87.5, 80.5, 449.75, 306.25, 281.75, 87.5, 675.5,986.125, 306.25, 281.75]
>>> rounded = map(round, floats)
>>> print rounded
[25.0, 193.0, 282.0, 88.0, 80.0, 450.0, 306.0, 282.0, 88.0, 676.0, 986.0, 306.0, 282.0]
Upvotes: 2
Reputation: 4528
Simply use round
function for all list members with list comprehension :
myList = [round(x) for x in myList]
myList # [25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
If you want round
with certain presicion n
use round(x,n)
:
Upvotes: 24
Reputation: 3891
You can use python's built in round
function.
l = [25.0, 193.0, 281.75, 87.5, 80.5, 449.75, 306.25, 281.75, 87.5, 675.5,986.125, 306.25, 281.75]
list = [round(x) for x in l]
print(list)
The output is:
[25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
Upvotes: 2