printing - How do I print a list of doubles nicely in 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 s in x])
which produces:
0.00 1.21 1.01 0.51 -0.34 -0.34 0.49 0.00
the problem being -0.34 1 character longer 0.51, produces ragged left edges when printing several lists.
any better ideas?
i'd like:
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
to turn into:
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
and nicer if there built in or standard library way of doing this, since print ' '.join(["%.2f" % s s in x])
quite lot type.
simply adjust padding positive , negative numbers accordingly:
''.join([" %.2f" % s if s >= 0 else " %.2f" % s s in x]).lstrip()
Comments
Post a Comment