Rounding a list of floats into integers in Python
Asked Answered
M

8

16

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?

Microfiche answered 26/2, 2016 at 12:14 Comment(0)
L
24

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):

Linders answered 26/2, 2016 at 12:16 Comment(0)
A
13

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.

Ahem answered 26/2, 2016 at 12:18 Comment(2)
Are you sure you don't need a list conversion on your second method?Enlistee
@muyustan: You're right. At the time I wrote this answer, I was mostly using Python 2, where map returns a list already, but yeah, that's changed.Ahem
A
4

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]
Aldose answered 13/5, 2020 at 13:49 Comment(0)
P
2

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]
Precentor answered 26/2, 2016 at 12:17 Comment(0)
V
2

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]
Verrucose answered 26/2, 2016 at 12:19 Comment(0)
M
2

NumPy is great for handling arrays like this.
Simply np.around(list) or np.round(list) works.

Mccormac answered 18/7, 2018 at 13:12 Comment(1)
Possibly also helpful to others reading on this topic: np.rintPavilion
K
1

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))

Kellykellyann answered 21/1, 2020 at 17:44 Comment(0)
B
0

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]
Between answered 24/1 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.