在python

时间:2017-08-31 17:18:22

标签: python excel-formula

我希望python执行类似于MS Excel =ROUNDDOWN (number, num_digits)的操作。所以在我看来像这样的例子中

a = [3.5, 43.689, 113.225, 65.4545]

我试过这样:

a = [3.5, 43.689, 113.225, 65.4545]
b = [str(i).split(".") for i in a]

c = [i[0] for i in b]
d = [i[1][0] for i in b]

e = list(zip(c,d))
f = [float(".".join(i)) for i in e]

给出了我需要的输出:

>>> print (f)
[3.5, 43.6, 113.2, 65.4]

有没有更好,更简单的方法在Python中执行上述操作?

4 个答案:

答案 0 :(得分:3)

几乎和你一样:

PS C:\file_folder\sub_folder> $hash["126037AA"]


FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : user123
IsInherited       : True
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : admin
IsInherited       : True
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None                          
...

无论如何,请记住,您可以使用小数并使用其表示格式。

您可以使用数学a = [3.5, 43.689, 113.225, 65.4545] f = [float("{:.1f}".format(x)) for x in a] 进行变换,如下所示:

floor

这里有live example

答案 1 :(得分:2)

我认为这更像是Pythonic的方式:

from decimal import Decimal, ROUND_DOWN
Decimal(65.4545).quantize(Decimal('.0'), rounding=ROUND_DOWN)

答案 2 :(得分:1)

我认为你可以使用圆形表达式:

>>> a = [3.5, 43.689, 113.225, 65.4545]
>>> [float(Decimal(b).quantize(decimal.Decimal('.0'), rounding=decimal.ROUND_DOWN)) for b in a]
[3.5, 43.6, 113.2, 65.4]

我希望这可以帮到你;)

答案 3 :(得分:1)

如果这是你想要的话,

int向零舍去:

a = [3.5, 43.689, 113.225, 65.4545]
[int(x * 10) / 10 for x in a]

输出:

[3.5, 43.6, 113.2, 65.4]