user5896534
user5896534

Reputation: 113

In python, how do I print a table using double for loops?

Here is my 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)
for b in ylist:
     print b, " | "
     for a in xlist:
        print gcd(a,b)

I'm having trouble printing a table that will display on the top row 2-15 and on the left column the values 2-50. With a gcd table for each value from each row and each column.

Here is a sample of what I'm getting

| 2 3 4 5 6 7 8 9 10 11 12 13 14 15


2 |

2

1

2

Upvotes: 0

Views: 2968

Answers (3)

zondo
zondo

Reputation: 20336

You can have it much more concise with 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)) for a in xlist]) for 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 you want zeros at the beginning of each one-digit number, replace each occurence of %2d with %02d. You probably shouldn't print the header like that, but do it more like this:

from fractions import gcd
xlist = range(2, 16)
ylist = range(2, 51)
string = "   | " + " ".join(("%2d" % x) for x in xlist)
print(string)
print("-" * len(string))

print("\n".join(" ".join(["%2d | " % b] + [("%2d" % gcd(a, b)) for a in xlist]) for b in ylist))

This way, even if you change xlist or ylist, the table will still look good.

Upvotes: 2

art049
art049

Reputation: 131

You can specify what kind of character end the line using the end parameter in print.

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)
for b in ylist:
     print(b + " | ",end="")
     for a in xlist:
        print(gcd(a,b),end="")
     print("")#Newline

If you are using python 2.x, you need to add from __future__ import print_function to the top for this to work.

Upvotes: 0

Chris
Chris

Reputation: 1623

Your problem is that the python print statement adds a newline by itself.

One solution to this is to build up your own string to output piece by piece and use only one print statement per line of the table, like such:

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)
for b in ylist:
     output=str(b)+" | " #For each number in ylist, make a new string with this number
     for a in xlist:
        output=output+str(gcd(a,b))+" " #Append to this for each number in xlist
     print output #Print the string you've built up

Example output, by the way:

| 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

Upvotes: 0

Related Questions