为什么我总是得到错误 "list" object has no attribute 'tranpose'?
Why do I always get the error "list" object has no attribute 'tranpose'?
这是我的代码:
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
函数时,我总是得到 "the list object has no attribute matrixmult"。这是为什么?我已经在前面的代码中定义了它 no?
ma2_t=m2.transpose()
AttributeError: 'list' 对象没有属性 'transpose'
有人帮忙吗?
matrixmult()
的最后一行是 returning a list
。它应该 return 一个 Matrix
对象:
return Matrix(self.n_rows, other.n_cols, res)
同样在transpose
方法中,你应该有:
return Matrix(self.n_cols, self.n_rows, ma_transpose)
这是我的代码:
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
函数时,我总是得到 "the list object has no attribute matrixmult"。这是为什么?我已经在前面的代码中定义了它 no?
ma2_t=m2.transpose()
AttributeError: 'list' 对象没有属性 'transpose'
有人帮忙吗?
matrixmult()
的最后一行是 returning a list
。它应该 return 一个 Matrix
对象:
return Matrix(self.n_rows, other.n_cols, res)
同样在transpose
方法中,你应该有:
return Matrix(self.n_cols, self.n_rows, ma_transpose)