Create a matrix of matrices in python

1

Dear, I have a query, how can I make a board or array of matrices in Python ?, whereas the matrix that will form my board or main matrix is a class (has the taxes to be able to rotate and fill in data inside from that same matrix)

class matriz:
    def __init__(self):
        self.t = [[0,0,0],
                  [0,0,0],
                  [0,0,0]]
    def __str__(self):
        txt =""
        txt +=str(self.t[0][0])+str(self.t[0][1])+str(self.t[0][2])+"\n"
        txt +=str(self.t[1][0])+str(self.t[1][1])+str(self.t[1][2])+"\n"
        txt +=str(self.t[2][0])+str(self.t[2][1])+str(self.t[2][2])
        return txt 
    def casilla_vacia(self, f, c):
        libre=self.t[f][c]==0
        return libre 
    def rellenar_casilla(self,f,c,j):
        libre=self.casilla_vacia(f,c)
        if libre:
            self.t[f][c]=j
            return True
        else:
            return False

    def nada_libre(self):
        ocupado= ((not 0 in self.t[0]) and 
                  (not 0 in self.t[1]) and
                  (not 0 in self.t[2]))
        return ocupado 
    def girar_izquierda(self):
        new_m = [[0,0,0],
                 [0,0,0],
                 [0,0,0]]
        new_m[0][0]=self.t[0][2]
        new_m[1][0]=self.t[0][1]
        new_m[2][0]=self.t[0][0]
        new_m[0][1]=self.t[1][2]
        new_m[2][1]=self.t[1][0]
        new_m[0][2]=self.t[2][2]
        new_m[1][2]=self.t[2][1]
        new_m[2][2]=self.t[2][0]

        print(self) 
    def girar_derecha(self):
        new_ma = [[0,0,0],
                  [0,0,0],
                  [0,0,0]]
        new_ma[0][0]=self.t[2][0]
        new_ma[1][0]=self.t[2][1]
        new_ma[2][0]=self.t[2][2]
        new_ma[0][1]=self.t[1][0]
        new_ma[2][1]=self.t[1][2]
        new_ma[0][2]=self.t[0][0]
        new_ma[1][2]=self.t[0][1]
        new_ma[2][2]=self.t[0][2]

        print(self) 

tab= matriz()

basically I want to make a matrix, but in which each element of that matrix is a 3X3 matrix that is defined in the code above

    
asked by VICENTE DANIEL PALACIOS 20.05.2017 в 17:54
source

1 answer

0

You need to make a list of lists of objects:

array =[]
for count in range(3):
   row=[]
   for count in range(3):
     m = matriz()     
     row.append(m)
   array.append(row)

You can also get it with a List Comprehensions:

comparray=[[matriz() for x in range(3)] for r in range(3)]

If you want to get the array of your class you have to add a getter in your class for example:

def getArray(self):
   return self.t

then you can get them printed with a list like this

imparray=[[comparray[x][y].getArray() for x in range(3)] for y in range(3)]

[[[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]], [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]], [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]]

To make it look better you can use a pprint

import pprint
pprint.pprint(imparray)
[[[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]],
 [[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]],
 [[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]]
    
answered by 20.05.2017 / 19:35
source