在 Python 中将浮点数列表四舍五入为整数
Rounding a list of floats into integers in Python
我有一个数字列表,在我继续使用该列表之前需要将其四舍五入为整数。源列表示例:
[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]
我该怎么做才能保存这个所有数字都四舍五入为整数的列表?
只需对具有列表理解的所有列表成员使用 round
函数:
myList = [round(x) for x in myList]
myList # [25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
如果你想 round
有一定的预测 n
使用 round(x,n)
:
使用 map
函数的另一种方法。
您可以设置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]
您可以使用内置函数 round()
进行列表推导:
newlist = [round(x) for x in list]
您也可以使用内置函数 map()
:
newlist = list(map(round, list))
不过,我不建议使用 list
作为名称,因为您隐藏了内置类型。
您可以使用 python 的内置 round
函数。
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)
输出为:
[25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
NumPy 非常适合处理这样的数组。
只需 np.around(list)
或 np.round(list)
即可。
为 python3 更新此内容,因为其他答案利用了 python2 的 map
,其中 returns 和 list
,其中 python3' s map
returns 一个迭代器。您可以让 list
函数使用您的 map
对象:
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]
要以这种方式将 round
用于特定的 n
,您需要使用 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))
如果您要设置有效数字的位数,您可以这样做
new_list = list(map(lambda x: round(x,precision),old_list))
此外,如果你有一个列表列表,你可以做
new_list = [list(map(lambda x: round(x,precision),old_l)) for old_l in old_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]
我该怎么做才能保存这个所有数字都四舍五入为整数的列表?
只需对具有列表理解的所有列表成员使用 round
函数:
myList = [round(x) for x in myList]
myList # [25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
如果你想 round
有一定的预测 n
使用 round(x,n)
:
使用 map
函数的另一种方法。
您可以设置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]
您可以使用内置函数 round()
进行列表推导:
newlist = [round(x) for x in list]
您也可以使用内置函数 map()
:
newlist = list(map(round, list))
不过,我不建议使用 list
作为名称,因为您隐藏了内置类型。
您可以使用 python 的内置 round
函数。
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)
输出为:
[25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
NumPy 非常适合处理这样的数组。
只需 np.around(list)
或 np.round(list)
即可。
为 python3 更新此内容,因为其他答案利用了 python2 的 map
,其中 returns 和 list
,其中 python3' s map
returns 一个迭代器。您可以让 list
函数使用您的 map
对象:
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]
要以这种方式将 round
用于特定的 n
,您需要使用 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))
如果您要设置有效数字的位数,您可以这样做
new_list = list(map(lambda x: round(x,precision),old_list))
此外,如果你有一个列表列表,你可以做
new_list = [list(map(lambda x: round(x,precision),old_l)) for old_l in old_list]