| Expert Mod 2.5K+
P: 2,509
|
please I would like to write a fonction that takes as argument a matrix, 2 integers describing a cell coordinate and a value that set the corresponding cell to the value?????????
please help me
Maybe this will help: - # A simple matrix
-
# This matrix is a list of lists
-
# Column and row numbers start with 1
-
-
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 __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)
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
-
>>>
|