Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding a list of floats into integers in Python

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?

like image 894
mrzippy01 Avatar asked Feb 26 '16 12:02

mrzippy01


1 Answers

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.

like image 100
zondo Avatar answered Sep 25 '22 18:09

zondo