0

I have users inputting into my program the degree of a matrix (e.g 3x3 == X=3, Y=3) I then populate a list of 9 values that will make up my 3x3 matrix like so:

    total = x * y  # the x and y values inputted by the user
    i = 1
    while i <= total:
        matrix.append(i)
        i += 1

After I have this matrix populated I want to print it out such that I print values matrix[0], [1], [2] in the first row and so on. But I want to do this for any size matrix, I have tried this:

i = 1
j = 0
while j < total:
    print matrix[j]
    if not i == x:
        print ''
        i += 1
    else:
        print '\n'
        i = 1
    j += 1

With no luck as it prints each value on a new line regardless... Any help is appreciated, thanks!

3 Answers3

3

How about this:

x = 3
y = 3

matrix = [[(i+1)+(j*x) for i in range(x)] for j in range(y)]

for row in matrix:
    print ' '.join(map(str, row))

prints:

1 2 3
4 5 6
7 8 9

EDIT: as suggested in the comments, we can make this last line more flexible to show a proper "matrix" format as follows:

for row in matrix:
    print ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row))

This means that regardless of the size of x and y, we'll always space our values out evenly. For example if x=6 and y=6, it'll print:

 1  2  3  4  5  6
 7  8  9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36

if x=10 and y=10 it'll print:

  1   2   3   4   5   6   7   8   9  10
 11  12  13  14  15  16  17  18  19  20
 21  22  23  24  25  26  27  28  29  30
 31  32  33  34  35  36  37  38  39  40
 41  42  43  44  45  46  47  48  49  50
 51  52  53  54  55  56  57  58  59  60
 61  62  63  64  65  66  67  68  69  70
 71  72  73  74  75  76  77  78  79  80
 81  82  83  84  85  86  87  88  89  90
 91  92  93  94  95  96  97  98  99 100

and so on.

asongtoruin
  • 9,794
  • 3
  • 36
  • 47
0

You can replace your

print ""

with

print "",

in order to not add a newline to the end.

Alternatively, you could use the python 3 print function, which supports this as an argument. To do this, you should import __ future__ and call print like a function. See the code below as an example:

from __future__ import print_function
print("my string", end = "")

You can read more about using print as a function on the docs page.

Cole
  • 23
  • 3
0

This answer is based on this answer. That answer takes into account only the shape. To make it look like one matrix you can use box-drawing characters.

c, x, y = 1, 3, 3

matrix = [[(i+1)+(j*x) for i in range(x)] for j in range(y)]

for row in matrix:
    if c == 1:
        print u'\u250F', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u2513'
    elif c-y == 0:
        print u'\u2517', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u251B'
    else:
        print u'\u2503', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u2503'
    c += 1

Another possibility is:

x, y = 3, 3

matrix = [[(i+1)+(j*x) for i in range(x)] for j in range(y)]

print u'\u250F', ''.join(((2*x)-2)*' '), u'\u2513'
for row in matrix:
    print u'\u2503', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u2503'
print u'\u2517', ''.join(((2*x)-2)*' '), u'\u251B'

If you are trying to run this in cmd.exe, see this question.

Community
  • 1
  • 1
Juan T
  • 1,219
  • 1
  • 10
  • 21