Rounding a list of floats into integers in Python
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]
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)
:
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.