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
please help me
# 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)
>>> 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 >>>
def __iter__(self): for row in range(self.rows): for col in range(self.cols): yield (self.matrix, row, col)
>>> a = Matrix(3,3) >>> x = 1.5 >>> for i,r,c in a: ... x = x*2 ... i[r][c] = x ... >>> a Row 1 = [3.0, 6.0, 12.0] Row 2 = [24.0, 48.0, 96.0] Row 3 = [192.0, 384.0, 768.0] >>>
def __iter__(self): for row in range(self.rows): for col in range(self.cols): yield (self.matrix, row, col)
>>> a = Matrix(3,3) >>> x = 1.5 >>> for i,r,c in a: ... x = x*2 ... i[r][c] = x ... >>> a Row 1 = [3.0, 6.0, 12.0] Row 2 = [24.0, 48.0, 96.0] Row 3 = [192.0, 384.0, 768.0] >>>
Comment