4

I have a 2D list containing these values:

text = [[4, 3, 8, 9, 5, 1, 2, 7, 6], [8, 3, 4, 1, 5, 9, 6, 7, 2], 
[6, 1, 8, 7, 5, 3, 2, 9, 4], [6, 9, 8, 7, 5, 3, 2, 1, 4], 
[6, 1, 8, 7, 5, 3, 2, 1, 4], [6, 1, 3, 2, 9, 4, 8, 7, 5]]

For instance, text[i] should be printed like this:

4 3 8
9 5 1
2 7 6

But my matrix prints this:

   r = 6
   m = []
    for i in range(r):
        m.append([int(x) for x in text[i]])
    for i in m:
        print (i) 
>>
    4 3 8 9 5 1 2 7 6 
    8 3 4 1 5 9 6 7 2
    6 1 8 7 5 3 2 9 4 
    6 9 8 7 5 3 2 1 4 
    6 1 8 7 5 3 2 1 4 
    6 1 3 2 9 4 8 7 5 
pk3
  • 51
  • 1
  • 1
  • 2

5 Answers5

8

You can use numpy. First, convert your list into numpy array. Then, take an element and reshape it to 3x3 matrix.

import numpy as np

text = [[4, 3, 8, 9, 5, 1, 2, 7, 6], [8, 3, 4, 1, 5, 9, 6, 7, 2],
[6, 1, 8, 7, 5, 3, 2, 9, 4], [6, 9, 8, 7, 5, 3, 2, 1, 4],
[6, 1, 8, 7, 5, 3, 2, 1, 4], [6, 1, 3, 2, 9, 4, 8, 7, 5]]

text = np.array(text)

print text[0].reshape((3, 3))
print text[1].reshape((3, 3))

Output:

[[4 3 8]
 [9 5 1]
 [2 7 6]]

[[8 3 4]
 [1 5 9]
 [6 7 2]]

With numpy, you are really working with matrices

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
dragon2fly
  • 2,309
  • 19
  • 23
3
def print9_as_3x3(list9):
    for i in 0, 3, 6:
        for j in 0, 1, 2:
            print(list9[i+j], end= ' ')
    print()

This can of course be easily generalized (using ranges instead of those literal tuples) but I'm trying to keep it as simple as feasible.

The Q's subject talks about creating matrices, which this code doesn't do, but the Q's body's all about printing instead, so I've focused on that! Please clarify (edit your Q) and comment if you want something different...

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
2

Use the grouper recipe referred to here to split each list into 3's, then print each set of three.

def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return itertools.zip_longest(*args, fillvalue=fillvalue)
    # zip_longest is new in Python3

for matrix in text:
    for chunk in grouper(matrix, 3):
        print(chunk)
    print() # empty line in between each 3x3

RESULT:

(4, 3, 8)
(9, 5, 1)
(2, 7, 6)

(8, 3, 4)
(1, 5, 9)
(6, 7, 2)

(6, 1, 8)
(7, 5, 3)
(2, 9, 4)

(6, 9, 8)
(7, 5, 3)
(2, 1, 4)

(6, 1, 8)
(7, 5, 3)
(2, 1, 4)

To make the literal output from the question, you'll have to do a bit of formatting but it shouldn't be too bad. Something like:

for matrix in text:
    for chunk in grouper(matrix, 3):
        print(" ".join(map(str, chunk)))
    print()

RESULT:

4 3 8
9 5 1
2 7 6

8 3 4
1 5 9
6 7 2

6 1 8
7 5 3
2 9 4

6 9 8
7 5 3
2 1 4

6 1 8
7 5 3
2 1 4

6 1 3
2 9 4
8 7 5
Community
  • 1
  • 1
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0
for a, b ,c in (x[i:i+3] for x in text for i in range(0,len(x),3)):
    print(a,b,c)

4 3 8
9 5 1
2 7 6
8 3 4
1 5 9
6 7 2
6 1 8
7 5 3
2 9 4
6 9 8
7 5 3
2 1 4
6 1 8
7 5 3
2 1 4
6 1 3
2 9 4
8 7 5
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

It's not completely clear to me what you're after, so here's one guess:

text = [[4, 3, 8, 9, 5, 1, 2, 7, 6], [8, 3, 4, 1, 5, 9, 6, 7, 2],
        [6, 1, 8, 7, 5, 3, 2, 9, 4], [6, 9, 8, 7, 5, 3, 2, 1, 4],
        [6, 1, 8, 7, 5, 3, 2, 1, 4], [6, 1, 3, 2, 9, 4, 8, 7, 5]]

# convert to 3D list in-place
for i, data in enumerate(text):
    text[i] = [[data[0], data[1], data[2]],
               [data[3], data[4], data[5]],
               [data[6], data[7], data[8]]]

# utility for print a 3x3 matrix         
def formatrix(label, matrix):
    indent = len(label)
    return '\n'.join((label + ', '.join(map(str, matrix[0])),
                      indent*' ' + ', '.join(map(str, matrix[1])),
                      indent*' ' + ', '.join(map(str, matrix[2]))))

# print each matrix in reformatted list
for i, matrix in enumerate(text):
    print(formatrix('text[%s]: ' % i, matrix))

Output:

text[0]: 4, 3, 8
         9, 5, 1
         2, 7, 6
text[1]: 8, 3, 4
         1, 5, 9
         6, 7, 2
text[2]: 6, 1, 8
         7, 5, 3
         2, 9, 4
text[3]: 6, 9, 8
         7, 5, 3
         2, 1, 4
text[4]: 6, 1, 8
         7, 5, 3
         2, 1, 4
text[5]: 6, 1, 3
         2, 9, 4
         8, 7, 5
martineau
  • 119,623
  • 25
  • 170
  • 301