Xecron
Xecron

Reputation: 31

How do I create a List of lists (matrix) containing different variables in Python?

I am currently working with the Python language and I am trying to create a class that acts as a matrix containing 4 parallel lists with a length that is equal to the length of a given sentence. Each list will have its own row and will contain different variables. However I am having troubles trying to print out each individual letter of the sentence into row 2. How would I go about achieving this through iteration (rather than manually appending them)?

For visual purposes, the end result will need to look like this:

Row 1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Row 2 = [h, e, l, l, o,  , w, o, r, l, d]
Row 3 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Row 4 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Below is my current code.

class Matrix(object):#,Sentence, Cipher_Sentence
    def __init__ (self, cols, rows):#, Nav):#, Guess, Crypt, Sent):
        self.cols=cols
        self.rows=rows
        self.sentence=sentence
        self.matrix=[]
        for i in range (rows):
            ea_row=[]
            for j in range (cols):
                ea_row.append(0)
            self.matrix.append(ea_row)

    def set_Nav(self, col, row, Nav):
        self.matrix[col-1][0]=Nav
    def get_Nav(self, col, row):
        return self.matrix[col-1][0]

    def set_Guess(self, col, row, Guess):
        self.matrix[col-1][1]=Guess
    def get_Guess(self, col, row):
        return self.matrix[col-1][1]

    def set_Crypt(self, col, row, Crypt):
        self.matrix[col-1][2]=Crypt
    def get_Crypt(self, col, row):
        return self.matrix[col-1][2]

    def set_Sent(self, col, row, Sent):
        self.matrix[col-1][3]=Sent
    def get_Sent(self, col, row):
        return self.matrix[col-1][3]

    def __repr__(self):
        rowname= ""
        for i in range(self.rows):
            rowname += 'Row %s = %s\n' %(i+1, self.matrix[i])

        return rowname
sentence="hello world"
m=Matrix(len(sentence),4)
print(m)

Thanks in advance

Upvotes: 1

Views: 86

Answers (1)

Paul
Paul

Reputation: 10863

OK, I don't totally understand your question, but I'm thinking that the gist of what you are getting at is that you want to set the entire row all at once, rather than going letter by letter.

Assuming that what you want set_Guess to do is set the row-th row to Guess, then you can change it to something like this:

def set_Guess(self, row, Guess):
    self.matrix[row][:] = Guess
def get_Guess(self, row):
    return self.matrix[row][:]

So, basically:

def set_Guess(matrix, row, Guess):
    matrix[row][:] = Guess
    return matrix

def get_Guess(matrix, row):
    return matrix[row][:]

sentence = "hello world"
rows = 4
cols = len(sentence)
matrix = [['0'] * cols for row in range(rows)]

set_Guess(matrix, 1, "hello world")

for rn, row in enumerate(matrix):
    print('Row {rnum:d} = [{rvals}]'.format(rnum=rn+1, rvals=', '.join(row)))

This returns what you were looking for:

> python testmatrix.py
Row 1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Row 2 = [h, e, l, l, o,  , w, o, r, l, d]
Row 3 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Row 4 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

That said, I suspect that for your particular application, you're not going about things the right way. I suggest maybe taking your application over to Code Review to get more help with the design.

Upvotes: 1

Related Questions