O
Odysseus
Hello, group: I've just begun some introductory tutorials in Python.
Taking off from the "word play" exercise at
<http://www.greenteapress.com/thinkpython/html/book010.html#toc96>
I've written a mini-program to tabulate the number of characters in each
word in a file. Once the data have been collected in a list, the output
is produced by a while loop that steps through it by incrementing an
index "i", saying
print '%2u %6u %4.2f' % \
(i, wordcounts, 100.0 * wordcounts / wordcounts[0])
My problem is with the last entry in each line, which isn't getting
padded:
1 0 0.00
2 85 0.07
3 908 0.80
4 3686 3.24
5 8258 7.26
6 14374 12.63
7 21727 19.09
8 26447 23.24
9 16658 14.64
10 9199 8.08
11 5296 4.65
12 3166 2.78
13 1960 1.72
14 1023 0.90
15 557 0.49
16 261 0.23
17 132 0.12
18 48 0.04
19 16 0.01
20 5 0.00
21 3 0.00
I've tried varying the number before the decimal in the formatting
string; "F", "g", and "G" conversions instead of "f"; and a couple of
other permutations (including replacing the arithmetical expression in
the tuple with a variable, defined on the previous line), but I can't
seem to get the decimal points to line up. I'm sure I'm missing
something obvious, but I'd appreciate a tip -- thanks in advance!
FWIW I'm running
Python 2.3.5 (#1, Oct 5 2005, 11:07:27)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
from the Terminal on Mac OS X v10.4.11.
P.S. Is there a preferable technique for forcing floating-point division
of two integers to that used above, multiplying by "100.0" first? What
about if I just wanted a ratio: is "float(n / m)" better than "1.0 * n /
m"?
Taking off from the "word play" exercise at
<http://www.greenteapress.com/thinkpython/html/book010.html#toc96>
I've written a mini-program to tabulate the number of characters in each
word in a file. Once the data have been collected in a list, the output
is produced by a while loop that steps through it by incrementing an
index "i", saying
print '%2u %6u %4.2f' % \
(i, wordcounts, 100.0 * wordcounts / wordcounts[0])
My problem is with the last entry in each line, which isn't getting
padded:
1 0 0.00
2 85 0.07
3 908 0.80
4 3686 3.24
5 8258 7.26
6 14374 12.63
7 21727 19.09
8 26447 23.24
9 16658 14.64
10 9199 8.08
11 5296 4.65
12 3166 2.78
13 1960 1.72
14 1023 0.90
15 557 0.49
16 261 0.23
17 132 0.12
18 48 0.04
19 16 0.01
20 5 0.00
21 3 0.00
I've tried varying the number before the decimal in the formatting
string; "F", "g", and "G" conversions instead of "f"; and a couple of
other permutations (including replacing the arithmetical expression in
the tuple with a variable, defined on the previous line), but I can't
seem to get the decimal points to line up. I'm sure I'm missing
something obvious, but I'd appreciate a tip -- thanks in advance!
FWIW I'm running
Python 2.3.5 (#1, Oct 5 2005, 11:07:27)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
from the Terminal on Mac OS X v10.4.11.
P.S. Is there a preferable technique for forcing floating-point division
of two integers to that used above, multiplying by "100.0" first? What
about if I just wanted a ratio: is "float(n / m)" better than "1.0 * n /
m"?