我有一个花车 list 。如果我只是 print
它,它会显示如下:
[9.0, 0.052999999999999999, 0.032575399999999997, 0.010892799999999999, 0.055702500000000002, 0.079330300000000006]
我可以使用 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]
最佳答案
由于没有人添加它,应该注意的是,从 Python 2.6+ 开始,推荐的方法是 string formating与 format
, 为 Python 3+ 做好准备。
print ["{0:0.2f}".format(i) for i in a]
The new string formating syntax不难用,但功能强大。
我虽然可能是 pprint
可能有一些东西,但我没有找到任何东西。
https://stackoverflow.com/questions/1566936/