sta*_*tti 82 python printing floating-point formatting
我有一个花车清单.如果我只是print它,它会显示如下:
[9.0, 0.052999999999999999, 0.032575399999999997, 0.010892799999999999, 0.055702500000000002, 0.079330300000000006]
Run Code Online (Sandbox Code Playgroud)
我可以使用print "%.2f",这需要一个for循环来遍历列表,但是它不适用于更复杂的数据结构.我想要的东西(我完全是这样做的)
>>> import print_options
>>> print_options.set_float_precision(2)
>>> print [9.0, 0.052999999999999999, 0.032575399999999997, 0.010892799999999999, 0.055702500000000002, 0.079330300000000006]
[9.0, 0.05, 0.03, 0.01, 0.06, 0.08]
Run Code Online (Sandbox Code Playgroud)
Est*_*ber 93
由于没有人添加它,应该注意的是,从Python 2.6+开始进行字符串格式化的推荐方法是format为Python 3+做好准备.
print ["{0:0.2f}".format(i) for i in a]
Run Code Online (Sandbox Code Playgroud)
我虽然pprint可能有东西,但我没有找到任何东西.
Rob*_*ney 76
一个更永久的解决方案是子类float:
>>> class prettyfloat(float):
def __repr__(self):
return "%0.2f" % self
>>> x
[1.290192, 3.0002, 22.119199999999999, 3.4110999999999998]
>>> x = map(prettyfloat, x)
>>> x
[1.29, 3.00, 22.12, 3.41]
>>> y = x[2]
>>> y
22.12
Run Code Online (Sandbox Code Playgroud)
子类化的问题float在于它会破坏明确寻找变量类型的代码.但据我所知,这是唯一的问题.一个简单的x = map(float, x)撤消转换为prettyfloat.
可悲的是,你不能只是猴子补丁float.__repr__,因为它float是不可改变的.
如果你不想继承子类float,但不介意定义一个函数,map(f, x)则要简洁得多[f(n) for n in x]
Pab*_*ruz 37
你可以做:
a = [9.0, 0.052999999999999999, 0.032575399999999997, 0.010892799999999999, 0.055702500000000002, 0.079330300000000006]
print ["%0.2f" % i for i in a]
Run Code Online (Sandbox Code Playgroud)
dal*_*ogm 22
请注意,您还可以将字符串乘以"%.2f"(例如:"%.2f"*10).
>>> print "%.2f "*len(yourlist) % tuple(yourlist)
2.00 33.00 4.42 0.31
Run Code Online (Sandbox Code Playgroud)
Pla*_*ong 17
这是一个老问题,但我会添加一些可能有用的东西:
我知道你在原始Python列表中编写了你的例子,但是如果你决定使用numpy数组(这在你的例子中是完全合法的,因为你似乎在处理数字数组),那么(几乎就是)这个命令你说你补了:
import numpy as np
np.set_printoptions(precision=2)
Run Code Online (Sandbox Code Playgroud)
或者在你的情况下甚至更好,如果你仍然想要查看真正精确数字的所有小数,但是除去尾随的零,例如,使用格式化字符串%g:
np.set_printoptions(formatter={"float_kind": lambda x: "%g" % x})
Run Code Online (Sandbox Code Playgroud)
要仅打印一次而不更改全局行为,请使用np.array2string与上述相同的参数.
print "[%s]"%", ".join(map(str,yourlist))
Run Code Online (Sandbox Code Playgroud)
这将避免打印时二进制表示中的舍入误差,而不会引入固定的精度约束(如格式化"%.2f"):
[9.0, 0.053, 0.0325754, 0.0108928, 0.0557025, 0.0793303]
Run Code Online (Sandbox Code Playgroud)
小智 8
l = [9.0, 0.052999999999999999, 0.032575399999999997, 0.010892799999999999, 0.055702500000000002, 0.079330300000000006]
Run Code Online (Sandbox Code Playgroud)
蟒蛇2:
print ', '.join('{:0.2f}'.format(i) for i in l)
Run Code Online (Sandbox Code Playgroud)
蟒蛇3:
print(', '.join('{:0.2f}'.format(i) for i in l))
Run Code Online (Sandbox Code Playgroud)
输出:
9.00, 0.05, 0.03, 0.01, 0.06, 0.08
Run Code Online (Sandbox Code Playgroud)
要控制有效数字的数量,请使用格式说明符 %g。
让我们将 Emile 的解决方案命名为 prettylist2f。这是修改后的一个:
prettylist2g = lambda l : '[%s]' % ', '.join("%.2g" % x for x in l)
Run Code Online (Sandbox Code Playgroud)
用法:
>>> c_e_alpha_eps0 = [299792458., 2.718281828, 0.00729735, 8.8541878e-12]
>>> print(prettylist2f(c_e_alpha_eps0)) # [299792458.00, 2.72, 0.01, 0.00]
>>> print(prettylist2g(c_e_alpha_eps0)) # [3e+08, 2.7, 0.0073, 8.9e-12]
Run Code Online (Sandbox Code Playgroud)
如果您希望有效数字的数量具有灵活性,请改用f 字符串格式:
prettyflexlist = lambda p, l : '[%s]' % ', '.join(f"{x:.{p}}" for x in l)
print(prettyflexlist(3,c_e_alpha_eps0)) # [3e+08, 2.72, 0.0073, 8.85e-12]
Run Code Online (Sandbox Code Playgroud)
最简单的选择应该是使用舍入例程:
import numpy as np
x=[9.0, 0.052999999999999999, 0.032575399999999997, 0.010892799999999999, 0.055702500000000002, 0.079330300000000006]
print('standard:')
print(x)
print("\nhuman readable:")
print(np.around(x,decimals=2))
Run Code Online (Sandbox Code Playgroud)
这会产生输出:
standard:
[9.0, 0.053, 0.0325754, 0.0108928, 0.0557025, 0.0793303]
human readable:
[ 9. 0.05 0.03 0.01 0.06 0.08]
Run Code Online (Sandbox Code Playgroud)