string formatting - printing and formatted list in python -


i using following code

print "line 1     line2" h, m in zip(human_score, machine_score):     print "{:5.1f}      {:5.3f}".format(h,m) 

but might not practice use spaces between "line 1" , "line 2" in header. , i'm not sure how add variable amount of spaces before each row can fit "mean" , "std" @ bottom, , have 2 numbers in line list above it.

for example, printed this:

       line 1      line 2          -6.0      7.200          -5.0      6.377         -10.0      14.688          -5.0      2.580          -8.0      8.421          -3.0      2.876          -6.0      9.812          -8.0      6.218          -8.0      15.873           7.5      -2.805 mean:  -0.026      7.26  std:    2.918      6.3 

what's pythonic way of doing this?

simply use larger field sizes, e.g., header use:

print "{:>17} {:>17s}".format('line1', 'line2') 

and numbers:

print "{:>17.1f}      {:>12.3f}".format(h,m) 

your footer:

print  print "mean: {:11.2f}      {:12.3f}".format(-0.026, 7.26) print "std : {:11.2f}      {:12.3f}".format(2.918, 6.3) 

which give you

            line1             line2              -6.0             7.200              -5.0             6.377             -10.0            14.688              -5.0             2.580              -8.0             8.421              -3.0             2.876              -6.0             9.812              -8.0             6.218              -8.0            15.873               7.5            -2.805  mean:       -0.03             7.260 std :        2.92             6.300 

you can adjust field width values according needs.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -