对象没有属性怎么解决
代码运行不了,给出来的是对象没有属性要求是:已知两个稀疏矩阵A和B,试基于三元组顺序表或十字链表的存储;链表编程实现A+B的运算
class TripleNode(object):
def __init__(self,row=0,column=0,value=0):
self.row = row
self.column = column
self.value = value
class SparseMatrix(object):
def __init__(self,maxSize):
self.maxSize = maxSize
self.data = * self.maxSize#三元组表
for i in range(self.maxSize):
self.data = TripleNode()
self.rows = 0 #行数
self.cols = 0 #列数
self.nums = 0 #非零元素个数
def create(self,mat):
count =k = 0
self.rows = len(mat)
self.cols = len(mat)
for i in range(self.rows):
for j in range(self.cols):
if mat !=0:
count +=1
self.num = count
self.data = * self.nums
for i in range(self.rows):
for j in range(self.cols):
if mat !=0:
self.data = TripleNode(i,j,mat)
k +=1
def display(self):
for t in range(self.nums):
print('(row:%s,column:%s,value:%s)' % (self.data.row,self.data.column,self.data.value),end=' ')
print('/n')
def ms_add(self,mat1,mat2):
k = count1=count2=0
self.rows = len(mat1)
self.cols = len(mat1)
for i in range(self.rows):
for j in range(self.cols):
if mat1 !=0:
count1 += 1
if mat2 !=0:
count2 +=1
self.nums = count1+count2
self.data = * self.nums
for i in range(self.rows):
for j in range(self.cols):
if mat1 != 0 and mat2 == 0:
self.data = TripleNode(i,j,mat1)
k+=1
elif mat1 == 0 and mat2 != 0:
self.data = TripleNode(i,j,mat2)
k+=1
elif mat1 != 0 and mat2 != 0:
self.data = TripleNode(i,j,mat1+mat2)
k+=1
self.nums=k
M1 = [
,
,
]
M2 = [
,
,
]
# print(len(M1))
# print(len(M1))
B1=SparseMatrix(100)
B1.create(M1)
B2=SparseMatrix(100)
B2.create(M2)
B3=SparseMatrix(100)
B3.ms_add(M1,M2)
print("矩阵A的三元组: ")
B1.dispaly()
print("矩阵B的三元组: ")
B2.dispaly()
print("矩阵A和矩阵B相加后的矩阵C的三元组: ")
B3.dispaly()
# B1.dispaly()
# B2.dispaly() 本帖最后由 isdkz 于 2023-4-3 10:44 编辑
三个问题:
1、create方法缩进错误
2、第25行的self.num应改为self.nums
3、第91、93、95行的dispaly 应改为 self.display
修正后的代码:
class TripleNode(object):
def __init__(self,row=0,column=0,value=0):
self.row = row
self.column = column
self.value = value
class SparseMatrix(object):
def __init__(self,maxSize):
self.maxSize = maxSize
self.data = * self.maxSize#三元组表
for i in range(self.maxSize):
self.data = TripleNode()
self.rows = 0 #行数
self.cols = 0 #列数
self.nums = 0 #非零元素个数
def create(self,mat):
count =k = 0
self.rows = len(mat)
self.cols = len(mat)
for i in range(self.rows):
for j in range(self.cols):
if mat !=0:
count +=1
self.nums = count
self.data = * self.nums
for i in range(self.rows):
for j in range(self.cols):
if mat !=0:
self.data = TripleNode(i,j,mat)
k +=1
def display(self):
for t in range(self.nums):
print('(row:%s,column:%s,value:%s)' % (self.data.row,self.data.column,self.data.value),end=' ')
print('/n')
def ms_add(self,mat1,mat2):
k = count1=count2=0
self.rows = len(mat1)
self.cols = len(mat1)
for i in range(self.rows):
for j in range(self.cols):
if mat1 !=0:
count1 += 1
if mat2 !=0:
count2 +=1
self.nums = count1+count2
self.data = * self.nums
for i in range(self.rows):
for j in range(self.cols):
if mat1 != 0 and mat2 == 0:
self.data = TripleNode(i,j,mat1)
k+=1
elif mat1 == 0 and mat2 != 0:
self.data = TripleNode(i,j,mat2)
k+=1
elif mat1 != 0 and mat2 != 0:
self.data = TripleNode(i,j,mat1+mat2)
k+=1
self.nums=k
M1 = [
,
,
]
M2 = [
,
,
]
# print(len(M1))
# print(len(M1))
B1=SparseMatrix(100)
B1.create(M1)
B2=SparseMatrix(100)
B2.create(M2)
B3=SparseMatrix(100)
B3.ms_add(M1,M2)
print("矩阵A的三元组: ")
B1.display()
print("矩阵B的三元组: ")
B2.display()
print("矩阵A和矩阵B相加后的矩阵C的三元组: ")
B3.display()
# B1.dispaly()
# B2.dispaly() {:5_106:}
页:
[1]