English 中文(简体)
如何在浮标到2个 de点的清单上逐项?
原标题:How to round each item in a list of floats to 2 decimal places?
  • 时间:2011-03-16 13:37:06
  •  标签:
  • python

我有一个由浮动价值组成的清单,但过于详细。 我知道,我们可以通过使用<代码>(>%.f”%变量)的操作者,如:

result = [359.70000000000005]
result = "%.2f" % result
result = [359.70]

我的问题是,我怎样才能把价值清单变成其四舍五入的等数,而不用使用一个炉子。 我曾尝试过一些东西,但投下了:

list = [0.30000000000000004, 0.5, 0.20000000000000001]
list = "%.2f" % list
TypeError: not all arguments converted during string formatting

我怎么能提供这样一个清洁的清单:

list = [0.30, 0.5, 0.20]
最佳回答

<代码>%.2f>不退还clean浮动。 该浮标有2个小数。

my_list = [0.30000000000000004, 0.5, 0.20000000000000001]
my_formatted_list = [  %.2f  % elem for elem in my_list ]

返回:

[ 0.30 ,  0.50 ,  0.20 ]

此外,不打上你可变的<代码> >list。 这是为编制清单而保留的一个词。 使用其他一些名称,例如my_list

如果您希望获得<代码>[0.30,0.5, 0.20](或至少是最接近的浮动),你可以尝试:

my_rounded_list = [ round(elem, 2) for elem in my_list ]

返回:

[0.29999999999999999, 0.5, 0.20000000000000001]
问题回答

如果你真的想要一种无休止的解决办法,那么你可以使用 n及其

import numpy as np
myList = list(np.around(np.array(myList),2))

You might want to look at Python s decimal module, which can make using floating point numbers and doing arithmetic with them a lot more intuitive. Here s a trivial example of one way of using it to "clean up" your list values:

>>> from decimal import *
>>> mylist = [0.30000000000000004, 0.5, 0.20000000000000001]
>>> getcontext().prec = 2
>>> ["%.2f" % e for e in mylist]
[ 0.30 ,  0.50 ,  0.20 ]
>>> [Decimal("%.2f" % e) for e in mylist]
[Decimal( 0.30 ), Decimal( 0.50 ), Decimal( 0.20 )]
>>> data = [float(Decimal("%.2f" % e)) for e in mylist]
>>> data
[0.3, 0.5, 0.2]
mylist = [0.30000000000000004, 0.5, 0.20000000000000001]
myRoundedList =  [round(x,2) for x in mylist] 
# [0.3, 0.5, 0.2]

Another option which doesn t require numpy is:

precision = 2  
myRoundedList = [int(elem*(10**precision)+delta)/(10.0**precision) for elem in myList]

# delta=0 for floor
# delta = 0.5 for round
# delta = 1 for ceil

www.un.org/Depts/DGACM/index_spanish.htm 最容易的方式:

进口 n

页: 1

Python3中最简单的方法是使用<代码>numpy.round(),以形成一个四舍五入的阵列和list(>)将其重新列入一个名单。 不必在将清单发送至<代码>numpy.round()之前将清单插入一个阵列。

import numpy as np

my_list = [0.342514, 0.52341234, 0.21234354346]

rounded_list = list(np.round(my_list, 2))

产出:<代码>[0.34,0.52, 0.21]

如果你不想把 n作为依赖物,那么 e蒸炉解决办法就是最好的选择。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...