本土解决方案:重量级 h2>
假设您想自己实现它,您将设置以下机制,这将允许您定义任意成对操作:
from pprint import pformat as pf
class Matrix(object):
def __init__(self, arrayOfRows=None, rows=None, cols=None):
if arrayOfRows:
self.data = arrayOfRows
else:
self.data = [[0 for c in range(cols)] for r in range(rows)]
self.rows = len(self.data)
self.cols = len(self.data[0])
@property
def shape(self): # myMatrix.shape -> (4,3)
return (self.rows, self.cols)
def __getitem__(self, i): # lets you do myMatrix[row][col
return self.data[i]
def __str__(self): # pretty string formatting
return pf(self.data)
@classmethod
def map(cls, func, *matrices):
assert len(set(m.shape for m in matrices))==1, 'Not all matrices same shape'
rows,cols =