如何在python中很好地打印双打列表? [英] How do I print a list of doubles nicely in python?

查看:138
本文介绍了如何在python中很好地打印双打列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经得到:

So far, I've got:

x=[0.0, 1.2135854798749774, 1.0069824713281044, 0.5141246736157659, -0.3396344921640888, -0.33926090064512615, 0.4877599543804152, 0.0]

print ' '.join(["%.2f" % s for s in x])

产生:

0.00 1.21 1.01 0.51 -0.34 -0.34 0.49 0.00

0.00 1.21 1.01 0.51 -0.34 -0.34 0.49 0.00

问题在于-0.34比0.51长一个字符,在打印多个列表时会产生参差不齐的左边缘.

the problem being that -0.34 is one character longer than 0.51, which produces ragged left edges when printing several lists.

还有更好的主意吗?

我想要:

0.00 1.21 1.01 0.51 -0.34 -0.34 0.49 0.00
0.00 1.21 1.01 0.51 0.34 0.34 0.49 0.00
0.00 1.21 -1.01 -0.51 -0.34 -0.34 -0.49 0.00

变成:

0.00 1.21  1.01  0.51 -0.34 -0.34  0.49 0.00
0.00 1.21  1.01  0.51  0.34  0.34  0.49 0.00
0.00 1.21 -1.01 -0.51 -0.34 -0.34 -0.49 0.00

,如果有一些内置的或标准库的方式可以做的更好,因为print ' '.join(["%.2f" % s for s in x])的输入量很大.

and it would be even nicer if there was some built in or standard library way of doing this, since print ' '.join(["%.2f" % s for s in x]) is quite a lot to type.

推荐答案

您是否尝试过"% .2f"(注意:空格)? –塞巴斯蒂安(J.F. Sebastian)

have you tried "% .2f" (note: the space)? – J.F. Sebastian

这篇关于如何在python中很好地打印双打列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆