Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numbers
- class Matrix(object):
- def __init__(self, seq, num_cols=1):
- self.m = seq
- self.num_cols = num_cols
- self.num_rows = len(seq) / num_cols
- def __getitem__(self, idx):
- row, col = idx
- if isinstance(row, numbers.Integral) and isinstance(col, numbers.Integral):
- return self.m[self.num_cols * row + col]
- elif isinstance(row, numbers.Integral) and isinstance(col, slice):
- start = self.num_cols * row
- stop = start + self.num_cols
- row_seq = self.m[slice(start, stop, 1)]
- return row_seq[col]
- elif isinstance(row, slice) and isinstance(col, numbers.Integral):
- start = col
- end = len(self.m) + col - self.num_cols + 1
- col_seq = self.m[slice(start, end, self.num_cols)]
- return col_seq[row]
- else:
- raise ValueError('\n\nOnly supports access for slicing like:\nM[1, 2], M[:, 0], M[2, :]\n\n')
- def __setitem__(self, key, value):
- raise NotImplementedError('Setting items not supported')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement