Skip to content Skip to sidebar Skip to footer

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' %

Solution 1:

Simply adjust the padding for positive and negative numbers accordingly:

''.join(["  %.2f" % s if s >= 0 else" %.2f" % s for s in x]).lstrip()

Solution 2:

Use rjust or ljust:

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

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

Solution 3:

try this: print ' '.join(["%5.2f" % s for s in x]), the number 5 in the string "%5.2f" specifies the maximum field width. It is compatible to the conversion specification in C.

Solution 4:

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

Post a Comment for "How Do I Print A List Of Doubles Nicely In Python?"