user2970916
user2970916

Reputation: 1186

Initialize a 2D Array in a Class in Python

I am trying to create a 3x3 Matrix class in python. I am having difficulty initializing the 2D array properly.

In the following code I am getting an error saying that Matrix3x3 has no attribute m. If I initialize m before the init, it seems like 'm' acts like a static variable, which I do not want.

class Matrix3x3:
    #[1 0 0]
    #[0 1 0]
    #[0 0 1]
    def __init__(self, m):
        #self.m[row][column]
        self.m[0][0] = m[0][0];
        self.m[0][1] = m[0][1];
        self.m[0][2] = m[0][2];

        self.m[1][0] = m[1][0];
        self.m[1][1] = m[1][1];
        self.m[1][2] = m[1][2];

        self.m[2][0] = m[2][0];
        self.m[2][1] = m[2][1];
        self.m[2][2] = m[2][2];

Upvotes: 0

Views: 16531

Answers (4)

Beri
Beri

Reputation: 11600

Try this:)

matrix_size = 3
Matrix = [[1 if x==y else 0 for y in xrange(matrix_size)] for x in xrange(matrix_size)]

Mayby is not 100% readeble at first, but works:)

Upvotes: 0

colevk
colevk

Reputation: 688

The problem is that m needs to exist before you can set its elements, and even then, trying to set the value of an element in a list will fail if the list is too short. So

lst = []
lst[0] = 0

will fail but

lst = []
lst.append(0)

will succeed. lst[0] = 0 should be thought of as trying to change the value of lst at index 0, and can't be used if lst has no index 0.

However, the simplest way to do what you want is either with list comprehensions,

class Matrix3x3:
    def __init__(self, m):
        self.m = [[m[i][j] for j in xrange(len(m[i]))] for i in xrange(len(m))]

or with deepcopy, if you don't plan on altering the input:

from copy import deepcopy

class Matrix3x3:
    def __init__(self, m):
        self.m = deepcopy(m)

Additionally, if this isn't for an assignment of some sort, you should consider instead using numpy.matrix, which has a bunch of built in matrix operations, rather than trying to roll your own.

Upvotes: 3

Serge Ballesta
Serge Ballesta

Reputation: 148880

You must allocate m as a list of lists before setting its elements :

class Matrix3x3:
    #[1 0 0]
    #[0 1 0]
    #[0 0 1]
    def __init__(self, m):
        #allocate the lists : m is a list containing 3 lists of 3 None values
        self.m = [ [ None for j in range(3) ] for i in range(3) ]
        for i in range(3):
            for j in range(3):
                self.m[i][j] = m[i][j];

or even better directly :

class Matrix3x3:
    #[1 0 0]
    #[0 1 0]
    #[0 0 1]
    def __init__(self, m):
        # directly init self.m
        self.m = [ [ m[i][j] for j in range(3) ] for i in range(3) ]

Upvotes: 0

TehTris
TehTris

Reputation: 3207

This class will generate a matrix 3x3

class Matrix3x3:
    def __init__(self):
        self.m = []
        for item in range(3):
            self.m.append([0,0,0])


mat = Matrix3x3()
print(mat.m)
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

it can be expanded upon to create X sized matrices

class Matrix_x_sized:
    def __init__(self,x):
        self.m = []
        for item in range(x):
            self.m.append([0]*x)

mat2 = Matrix_x_sized(4)
mat2.m
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Upvotes: -1

Related Questions