# A simple matrix

# This matrix is a list of lists

class Matrix(object):

def __init__(self, cols, rows):

self.cols = cols

self.rows = rows

# initialize matrix and fill with zeroes

self.matrix = []

for i in range(rows):

ea_row = []

for j in range(cols):

ea_row.append(0)

self.matrix.append(ea_row)

def setitem(self, col, row, v):

self.matrix[col-1][row-1] = v

def getitem(self, col, row):

return self.matrix[col-1][row-1]

def __iter__(self):

for row in range(self.rows):

for col in range(self.cols):

yield (self.matrix, row, col)

def __repr__(self):

outStr = ""

for i in range(self.rows):

outStr += 'Row %s = %s\n' % (i+1, self.matrix[i])

return outStr

a = Matrix(4,4)

print a

a.setitem(3,4,'55.75')

print a

a.setitem(2,3,'19.1')

print a

print a.getitem(3,4)

print

for i, r, c in a:

print i[r][c]

print

x = 1.5

for i,r,c in a:

x = x*2.0

print x

i[r][c] = x

print a

# Output

“””

>>> Row 1 = [0, 0, 0, 0]

Row 2 = [0, 0, 0, 0]

Row 3 = [0, 0, 0, 0]

Row 4 = [0, 0, 0, 0]

Row 1 = [0, 0, 0, 0]

Row 2 = [0, 0, 0, 0]

Row 3 = [0, 0, 0, '55.75']

Row 4 = [0, 0, 0, 0]

Row 1 = [0, 0, 0, 0]

Row 2 = [0, 0, '19.1', 0]

Row 3 = [0, 0, 0, '55.75']

Row 4 = [0, 0, 0, 0]

55.75

0

0

0

0

0

0

19.1

0

0

0

0

55.75

0

0

0

0

3.0

6.0

12.0

24.0

48.0

96.0

192.0

384.0

768.0

1536.0

3072.0

6144.0

12288.0

24576.0

49152.0

98304.0

Row 1 = [3.0, 6.0, 12.0, 24.0]

Row 2 = [48.0, 96.0, 192.0, 384.0]

Row 3 = [768.0, 1536.0, 3072.0, 6144.0]

Row 4 = [12288.0, 24576.0, 49152.0, 98304.0]

>>>

“””