Zach
Zach

Reputation: 4744

printing and formatted list in python

I am currently using the following code

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

But it might not be good practice to just use spaces between "line 1" and "line 2" in the header. And I'm not sure how to add a variable amount of spaces before each row so that I can fit "mean" and "std" at the bottom, and have those two numbers in line with the list above it.

For example, I would like it printed like 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 the most pythonic way of doing this?

Upvotes: 0

Views: 2150

Answers (5)

lavee_singh
lavee_singh

Reputation: 1459

Use str.rjust and str.ljust and relate this with the digits in the score.

Upvotes: 0

alan
alan

Reputation: 4852

Your original question was about how to avoid putting arbitrary spaces between the fields in the format string. You are right to try to avoid this. Even more flexibility comes with not hard-coding the padding width for the columns.

You can do both by using a WIDTH 'constant' defined outside the format string. The width then gets passed into the format function as a parameter, and inserted into the format string in another set of braces, inside the replacement field: {foo:>{width}}:

If you want to change the column width, just change the 'constant' WIDTH:

human_score = [1.23, 2.32,3.43,4.24]
machine_score = [0.23, 4.22,3.33,5.21]
WIDTH = 12
mean = "Mean:"
std = "Std:"
print '{0:>{width}}{1:>{width}}'.format('line 1', 'line 2', width=WIDTH)
for h, m in zip(human_score, machine_score):
    print "{:>{width}.1f}{:>{width}.3f}".format(h,m, width=WIDTH)

print "{mean}{:>{width1}.2f}{:>{width2}.3f}".format(-0.026, 7.26, width1=WIDTH-len(mean), width2=WIDTH, mean=mean)
print "{std}{:>{width1}.2f}{:>{width2}.3f}".format(-2.918, 6.3, width1=WIDTH-len(std), width2=WIDTH, std=std)

Outputs:

      line 1      line 2
         1.2       0.230
         2.3       4.220
         3.4       3.330
         4.2       5.210
Mean:  -0.03       7.260
Std:   -2.92       6.300

Upvotes: 1

Levon
Levon

Reputation: 143152

Simply use larger field sizes, e.g., for your header use:

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

and for your 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 would 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 the field width values according to your needs.

Upvotes: 2

Hedde van der Heide
Hedde van der Heide

Reputation: 22469

You could use ljust or rjust

Some examples diveintopython

Upvotes: 0

unwind
unwind

Reputation: 400129

Use the same printing technique for headers as you do for data, treating the header words as strings.

Upvotes: 1

Related Questions