In python, how do I print a table using double for loops? -
here python code,
from fractions import gcd print "| 2 3 4 5 6 7 8 9 10 11 12 13 14 15" print "-----------------------------------" xlist = range(2,16) ylist = range(2,51) b in ylist: print b, " | " in xlist: print gcd(a,b)
i'm having trouble printing table display on top row 2-15 , on left column values 2-50. gcd table each value each row , each column.
here sample of i'm getting
| 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 |
2
1
2
you can have more concise list comprehension:
from fractions import gcd print(" | 2 3 4 5 6 7 8 9 10 11 12 13 14 15") print("-----------------------------------------------") xlist = range(2,16) ylist = range(2,51) print("\n".join(" ".join(["%2d | " % b] + [("%2d" % gcd(a, b)) in xlist]) b in ylist))
output:
| 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ----------------------------------------------- 2 | 2 1 2 1 2 1 2 1 2 1 2 1 2 1 3 | 1 3 1 1 3 1 1 3 1 1 3 1 1 3 4 | 2 1 4 1 2 1 4 1 2 1 4 1 2 1 5 | 1 1 1 5 1 1 1 1 5 1 1 1 1 5 6 | 2 3 2 1 6 1 2 3 2 1 6 1 2 3 7 | 1 1 1 1 1 7 1 1 1 1 1 1 7 1 8 | 2 1 4 1 2 1 8 1 2 1 4 1 2 1 9 | 1 3 1 1 3 1 1 9 1 1 3 1 1 3 10 | 2 1 2 5 2 1 2 1 10 1 2 1 2 5 11 | 1 1 1 1 1 1 1 1 1 11 1 1 1 1 12 | 2 3 4 1 6 1 4 3 2 1 12 1 2 3 13 | 1 1 1 1 1 1 1 1 1 1 1 13 1 1 14 | 2 1 2 1 2 7 2 1 2 1 2 1 14 1 15 | 1 3 1 5 3 1 1 3 5 1 3 1 1 15 16 | 2 1 4 1 2 1 8 1 2 1 4 1 2 1 17 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 18 | 2 3 2 1 6 1 2 9 2 1 6 1 2 3 19 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 20 | 2 1 4 5 2 1 4 1 10 1 4 1 2 5 21 | 1 3 1 1 3 7 1 3 1 1 3 1 7 3 22 | 2 1 2 1 2 1 2 1 2 11 2 1 2 1 23 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 24 | 2 3 4 1 6 1 8 3 2 1 12 1 2 3 25 | 1 1 1 5 1 1 1 1 5 1 1 1 1 5 26 | 2 1 2 1 2 1 2 1 2 1 2 13 2 1 27 | 1 3 1 1 3 1 1 9 1 1 3 1 1 3 28 | 2 1 4 1 2 7 4 1 2 1 4 1 14 1 29 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 30 | 2 3 2 5 6 1 2 3 10 1 6 1 2 15 31 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 32 | 2 1 4 1 2 1 8 1 2 1 4 1 2 1 33 | 1 3 1 1 3 1 1 3 1 11 3 1 1 3 34 | 2 1 2 1 2 1 2 1 2 1 2 1 2 1 35 | 1 1 1 5 1 7 1 1 5 1 1 1 7 5 36 | 2 3 4 1 6 1 4 9 2 1 12 1 2 3 37 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 38 | 2 1 2 1 2 1 2 1 2 1 2 1 2 1 39 | 1 3 1 1 3 1 1 3 1 1 3 13 1 3 40 | 2 1 4 5 2 1 8 1 10 1 4 1 2 5 41 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 42 | 2 3 2 1 6 7 2 3 2 1 6 1 14 3 43 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 44 | 2 1 4 1 2 1 4 1 2 11 4 1 2 1 45 | 1 3 1 5 3 1 1 9 5 1 3 1 1 15 46 | 2 1 2 1 2 1 2 1 2 1 2 1 2 1 47 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 48 | 2 3 4 1 6 1 8 3 2 1 12 1 2 3 49 | 1 1 1 1 1 7 1 1 1 1 1 1 7 1 50 | 2 1 2 5 2 1 2 1 10 1 2 1 2 5
this works in python2 and python3. if want zeros @ beginning of each one-digit number, replace each occurence of %2d
%02d
. shouldn't print header that, more this:
from fractions import gcd xlist = range(2, 16) ylist = range(2, 51) string = " | " + " ".join(("%2d" % x) x in xlist) print(string) print("-" * len(string)) print("\n".join(" ".join(["%2d | " % b] + [("%2d" % gcd(a, b)) in xlist]) b in ylist))
this way, if change xlist
or ylist
, table still good.
Comments
Post a Comment