在Python中打印列表时控制打印格式

时间:2021-09-06 21:42:07

I have a list with floating point number named a. When I print the list with print a. I get the result as follows.

我有一个浮点数为a的列表,当我用a打印列表时,我得到如下结果。

[8.364, 0.37, 0.09300000000000003, 7.084999999999999, 0.469, 0.303, 9.469999999999999, 0.28600000000000003, 0.2290000000 000001, 9.414, 0.9860000000000001, 0.534, 2.1530000000000005]

[8.364, 0.37, 0.09300000000000003, 7.08499999999999999999999999999, 0.469, 0.303, 9.4699999999999999999, 0.2860000000000000000003, 0.229000001, 9.414, 0.986000000001, 0.534, 2.15300000000000000001]

Can I tell the list printer to print the format of "5.3f" to get better result?

我可以告诉列表打印机打印“5.3f”的格式以得到更好的结果吗?

[8.364, 0.37, 0.093, 7.084, 0.469, 0.303, 9.469, 0.286, 0.229, 9.414, 0.986, 0.534, 2.153]

(8.364,0.37,0.093,7.084,0.469,0.303,9.469,0.286,0.229,9.414,0.986,0.534,2.153)

2 个解决方案

#1


17  

In [4]: print ['%5.3f' % val for val in l]
['8.364', '0.370', '0.093', '7.085', '0.469', '0.303', '9.470', '0.286', '0.229', '1.000', '9.414', '0.986', '0.534', '2.153']

where l is your list.

我是你的名单。

edit: If the quotes are an issue, you could use

编辑:如果引用是一个问题,你可以使用

In [5]: print '[' + ', '.join('%5.3f' % v for v in l) + ']'
[8.364, 0.370, 0.093, 7.085, 0.469, 0.303, 9.470, 0.286, 0.229, 1.000, 9.414, 0.986, 0.534, 2.153]

#2


5  

If you need this to work in nested structures, you should look into the pprint module. The following should do what you want, in this context:

如果您需要它在嵌套结构中工作,您应该查看pprint模块。在此背景下,以下内容应符合您的要求:

from pprint import PrettyPrinter
class MyPrettyPrinter(PrettyPrinter):
    def format(self, object, context, maxlevels, level):
        if isinstance(object, float):
            return ('%.2f' % object), True, False
        else:
            return PrettyPrinter.format(self, object, context,
                                        maxlevels, level)
print MyPrettyPrinter().pprint({1: [8, 1./3, [1./7]]})
# displays {1: [8, 0.33, [0.14]]}

For more details on pprint, see: http://docs.python.org/library/pprint.html

有关pprint的详细信息,请参见:http://docs.python.org/library/pprint.html

If it's just for a flat list, then aix's solution is good. Or if you don't like the extra quotes, you could do:

如果只是一个平面列表,那么aix的解决方案是好的。或者如果你不喜欢额外的报价,你可以这样做:

print '[%s]' % ', '.join('%.3f' % val for val in list)

#1


17  

In [4]: print ['%5.3f' % val for val in l]
['8.364', '0.370', '0.093', '7.085', '0.469', '0.303', '9.470', '0.286', '0.229', '1.000', '9.414', '0.986', '0.534', '2.153']

where l is your list.

我是你的名单。

edit: If the quotes are an issue, you could use

编辑:如果引用是一个问题,你可以使用

In [5]: print '[' + ', '.join('%5.3f' % v for v in l) + ']'
[8.364, 0.370, 0.093, 7.085, 0.469, 0.303, 9.470, 0.286, 0.229, 1.000, 9.414, 0.986, 0.534, 2.153]

#2


5  

If you need this to work in nested structures, you should look into the pprint module. The following should do what you want, in this context:

如果您需要它在嵌套结构中工作,您应该查看pprint模块。在此背景下,以下内容应符合您的要求:

from pprint import PrettyPrinter
class MyPrettyPrinter(PrettyPrinter):
    def format(self, object, context, maxlevels, level):
        if isinstance(object, float):
            return ('%.2f' % object), True, False
        else:
            return PrettyPrinter.format(self, object, context,
                                        maxlevels, level)
print MyPrettyPrinter().pprint({1: [8, 1./3, [1./7]]})
# displays {1: [8, 0.33, [0.14]]}

For more details on pprint, see: http://docs.python.org/library/pprint.html

有关pprint的详细信息,请参见:http://docs.python.org/library/pprint.html

If it's just for a flat list, then aix's solution is good. Or if you don't like the extra quotes, you could do:

如果只是一个平面列表,那么aix的解决方案是好的。或者如果你不喜欢额外的报价,你可以这样做:

print '[%s]' % ', '.join('%.3f' % val for val in list)