在 Python 中将浮点数列表四舍五入为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35651470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:45:23  来源:igfitidea点击:

Rounding a list of floats into integers in Python

pythonlistrounding

提问by mrzippy01

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?

我该怎么做才能保存所有数字四舍五入为整数的列表?

采纳答案by ?????

Simply use roundfunction for all list members with list comprehension :

只需round对具有列表理解的所有列表成员使用函数:

myList = [round(x) for x in myList]

myList # [25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]

If you want roundwith certain presicion nuse round(x,n):

如果你想round有一定的presicionn使用round(x,n)

回答by Mauro Baraldi

Another approach using mapfunction.

另一种使用map函数的方法。

You can set how many digits to round.

您可以设置多少位数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]

回答by zondo

You could use the built-in function round()with a list comprehension:

您可以将内置函数round()与列表理解一起使用:

newlist = [round(x) for x in list]

You could also use the built-in function map():

您还可以使用内置函数map()

newlist = list(map(round, list))

I wouldn't recommend listas a name, though, because you are shadowing the built-in type.

list不过,我不建议将其用作名称,因为您正在隐藏内置类型。

回答by Mark Skelton

You can use python's built in roundfunction.

您可以使用 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)

The output is:

输出是:

[25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]

回答by Artem Yevtushenko

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

NumPy 非常适合处理这样的数组。
简单np.around(list)np.round(list)有效。

回答by C.Nivs

Updating this for python3 since other answers leverage python2's map, which returns a list, where python3's mapreturns an iterator. You can have the listfunction consume your mapobject:

为 python3 更新这个,因为其他答案利用 python2's map,它返回 a list,其中 python3'smap返回一个迭代器。您可以让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]

To use roundin this way for a specific n, you'll want to use functools.partial:

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

回答by brodegon

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]