鱼C论坛

 找回密码
 立即注册
查看: 1422|回复: 2

[已解决]对象没有属性怎么解决

[复制链接]
发表于 2023-4-3 10:31:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码运行不了,给出来的是对象没有属性
要求是:已知两个稀疏矩阵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 = [None] * self.maxSize  #三元组表
        for i in range(self.maxSize):
            self.data[i] = 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[0])
    for i in range(self.rows):
        for j in range(self.cols):
            if mat[i][j] !=0:
                count +=1
    self.num = count
    self.data = [None] * self.nums
    for i in range(self.rows):
        for j in range(self.cols):
            if mat[i][j] !=0:
                self.data[k] = TripleNode(i,j,mat[i][j])
                k +=1

    def display(self):
        for t in range(self.nums):
            print('(row:%s,column:%s,value:%s)' % (self.data[t].row,self.data[t].column,self.data[t].value),end=' ')
            print('/n')

    def ms_add(self,mat1,mat2):
        k = count1=count2=0
        self.rows = len(mat1)
        self.cols = len(mat1[0])
        for i in range(self.rows):
            for j in range(self.cols):
                if mat1[i][j] !=0:
                    count1 += 1
                if mat2[i][j] !=0:
                    count2 +=1
        self.nums = count1+count2
        self.data = [None] * self.nums
        for i in range(self.rows):
            for j in range(self.cols):
                if mat1[i][j] != 0 and mat2[i][j] == 0:
                    self.data[k] = TripleNode(i,j,mat1[i][j])
                    k+=1
                elif mat1[i][j] == 0 and mat2[i][j] != 0:
                    self.data[k] = TripleNode(i,j,mat2[i][j])
                    k+=1
                elif mat1[i][j] != 0 and mat2[i][j] != 0:
                    self.data[k] = TripleNode(i,j,mat1[i][j]+mat2[i][j])
                    k+=1
        self.nums=k





M1 = [
    [0,1,1],
    [0,0,0],
    [0,1,0]
]

M2 = [
    [0,1,1],
    [0,0,0],
    [0,1,0]
]

# print(len(M1))
# print(len(M1[0]))
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()
最佳答案
2023-4-3 10:43:14
本帖最后由 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 = [None] * self.maxSize  #三元组表
        for i in range(self.maxSize):
            self.data[i] = 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[0])
        for i in range(self.rows):
            for j in range(self.cols):
                if mat[i][j] !=0:
                    count +=1
        self.nums = count
        self.data = [None] * self.nums
        for i in range(self.rows):
            for j in range(self.cols):
                if mat[i][j] !=0:
                    self.data[k] = TripleNode(i,j,mat[i][j])
                    k +=1

    def display(self):
        for t in range(self.nums):
            print('(row:%s,column:%s,value:%s)' % (self.data[t].row,self.data[t].column,self.data[t].value),end=' ')
            print('/n')

    def ms_add(self,mat1,mat2):
        k = count1=count2=0
        self.rows = len(mat1)
        self.cols = len(mat1[0])
        for i in range(self.rows):
            for j in range(self.cols):
                if mat1[i][j] !=0:
                    count1 += 1
                if mat2[i][j] !=0:
                    count2 +=1
        self.nums = count1+count2
        self.data = [None] * self.nums
        for i in range(self.rows):
            for j in range(self.cols):
                if mat1[i][j] != 0 and mat2[i][j] == 0:
                    self.data[k] = TripleNode(i,j,mat1[i][j])
                    k+=1
                elif mat1[i][j] == 0 and mat2[i][j] != 0:
                    self.data[k] = TripleNode(i,j,mat2[i][j])
                    k+=1
                elif mat1[i][j] != 0 and mat2[i][j] != 0:
                    self.data[k] = TripleNode(i,j,mat1[i][j]+mat2[i][j])
                    k+=1
        self.nums=k





M1 = [
    [0,1,1],
    [0,0,0],
    [0,1,0]
]

M2 = [
    [0,1,1],
    [0,0,0],
    [0,1,0]
]

# print(len(M1))
# print(len(M1[0]))
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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-3 10:43:14 | 显示全部楼层    本楼为最佳答案   
本帖最后由 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 = [None] * self.maxSize  #三元组表
        for i in range(self.maxSize):
            self.data[i] = 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[0])
        for i in range(self.rows):
            for j in range(self.cols):
                if mat[i][j] !=0:
                    count +=1
        self.nums = count
        self.data = [None] * self.nums
        for i in range(self.rows):
            for j in range(self.cols):
                if mat[i][j] !=0:
                    self.data[k] = TripleNode(i,j,mat[i][j])
                    k +=1

    def display(self):
        for t in range(self.nums):
            print('(row:%s,column:%s,value:%s)' % (self.data[t].row,self.data[t].column,self.data[t].value),end=' ')
            print('/n')

    def ms_add(self,mat1,mat2):
        k = count1=count2=0
        self.rows = len(mat1)
        self.cols = len(mat1[0])
        for i in range(self.rows):
            for j in range(self.cols):
                if mat1[i][j] !=0:
                    count1 += 1
                if mat2[i][j] !=0:
                    count2 +=1
        self.nums = count1+count2
        self.data = [None] * self.nums
        for i in range(self.rows):
            for j in range(self.cols):
                if mat1[i][j] != 0 and mat2[i][j] == 0:
                    self.data[k] = TripleNode(i,j,mat1[i][j])
                    k+=1
                elif mat1[i][j] == 0 and mat2[i][j] != 0:
                    self.data[k] = TripleNode(i,j,mat2[i][j])
                    k+=1
                elif mat1[i][j] != 0 and mat2[i][j] != 0:
                    self.data[k] = TripleNode(i,j,mat1[i][j]+mat2[i][j])
                    k+=1
        self.nums=k





M1 = [
    [0,1,1],
    [0,0,0],
    [0,1,0]
]

M2 = [
    [0,1,1],
    [0,0,0],
    [0,1,0]
]

# print(len(M1))
# print(len(M1[0]))
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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-7 12:37:59 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 23:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表