这是我的代码:
class Matrix(object):
"""List of lists, where the lists are the rows of the matrix"""
def __init__ (self, m=3,n=3,ma=[[1,2,3],[4,5,6], [7,8,9]]):
self.n_rows=m
self.n_cols=n
self.matrix=ma=list(ma)
def __repr__(self):
"""String representation of a Matrix"""
output= "Matrix: \n{} \n Your matrix has \n {} columns \n {} rows"\
.format(self.matrix, self.n_cols, self.n_rows)
return output
def transpose (self):
"""Transpose the Matrix: Make rows into columns and columns into rows"""
ma_transpose=[]
for r in zip(*self.matrix):
ma_transpose.append(list(r))
return ma_transpose
def matrixmult (self,other):
"""Matrix Multiplication of 2 Matrices"""
m2=other.matrix
ma2_t=m2.transpose()
table=[]
for r in self.matrix:
row=[]
for n in ma2_t:
row.append(dot_mult(r,n))
table.append(row) into a list of row
res=table
return res
但每次我尝试使用matrixmult
函数时,我总是得到"列表对象没有属性matrixmult"。这是为什么?我之前在代码no中定义了它?
ma2_t=m2.transpose()
AttributeError:' list'对象没有属性'转置'
有人请帮忙吗?
答案 0 :(得分:1)
matrixmult()
的最后一行返回list
。它应该返回一个Matrix
对象:
return Matrix(self.n_rows, other.n_cols, res)
同样在transpose
方法中,您应该:
return Matrix(self.n_cols, self.n_rows, ma_transpose)