鱼C论坛

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

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

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

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

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

x
代码运行不了,给出来的是对象没有属性
要求是:已知两个稀疏矩阵A和B,试基于三元组顺序表或十字链表的存储;链表编程实现A+B的运算

  1. class TripleNode(object):
  2.     def __init__(self,row=0,column=0,value=0):
  3.         self.row = row
  4.         self.column = column
  5.         self.value = value

  6. class SparseMatrix(object):
  7.     def __init__(self,maxSize):
  8.         self.maxSize = maxSize
  9.         self.data = [None] * self.maxSize  #三元组表
  10.         for i in range(self.maxSize):
  11.             self.data[i] = TripleNode()
  12.         self.rows = 0 #行数
  13.         self.cols = 0 #列数
  14.         self.nums = 0 #非零元素个数

  15. def create(self,mat):
  16.     count =k = 0
  17.     self.rows = len(mat)
  18.     self.cols = len(mat[0])
  19.     for i in range(self.rows):
  20.         for j in range(self.cols):
  21.             if mat[i][j] !=0:
  22.                 count +=1
  23.     self.num = count
  24.     self.data = [None] * self.nums
  25.     for i in range(self.rows):
  26.         for j in range(self.cols):
  27.             if mat[i][j] !=0:
  28.                 self.data[k] = TripleNode(i,j,mat[i][j])
  29.                 k +=1

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

  34.     def ms_add(self,mat1,mat2):
  35.         k = count1=count2=0
  36.         self.rows = len(mat1)
  37.         self.cols = len(mat1[0])
  38.         for i in range(self.rows):
  39.             for j in range(self.cols):
  40.                 if mat1[i][j] !=0:
  41.                     count1 += 1
  42.                 if mat2[i][j] !=0:
  43.                     count2 +=1
  44.         self.nums = count1+count2
  45.         self.data = [None] * self.nums
  46.         for i in range(self.rows):
  47.             for j in range(self.cols):
  48.                 if mat1[i][j] != 0 and mat2[i][j] == 0:
  49.                     self.data[k] = TripleNode(i,j,mat1[i][j])
  50.                     k+=1
  51.                 elif mat1[i][j] == 0 and mat2[i][j] != 0:
  52.                     self.data[k] = TripleNode(i,j,mat2[i][j])
  53.                     k+=1
  54.                 elif mat1[i][j] != 0 and mat2[i][j] != 0:
  55.                     self.data[k] = TripleNode(i,j,mat1[i][j]+mat2[i][j])
  56.                     k+=1
  57.         self.nums=k





  58. M1 = [
  59.     [0,1,1],
  60.     [0,0,0],
  61.     [0,1,0]
  62. ]

  63. M2 = [
  64.     [0,1,1],
  65.     [0,0,0],
  66.     [0,1,0]
  67. ]

  68. # print(len(M1))
  69. # print(len(M1[0]))
  70. B1=SparseMatrix(100)
  71. B1.create(M1)

  72. B2=SparseMatrix(100)
  73. B2.create(M2)

  74. B3=SparseMatrix(100)
  75. B3.ms_add(M1,M2)

  76. print("矩阵A的三元组: ")
  77. B1.dispaly()
  78. print("矩阵B的三元组: ")
  79. B2.dispaly()
  80. print("矩阵A和矩阵B相加后的矩阵C的三元组: ")
  81. B3.dispaly()

  82. # B1.dispaly()
  83. # 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

修正后的代码:
  1. class TripleNode(object):
  2.     def __init__(self,row=0,column=0,value=0):
  3.         self.row = row
  4.         self.column = column
  5.         self.value = value

  6. class SparseMatrix(object):
  7.     def __init__(self,maxSize):
  8.         self.maxSize = maxSize
  9.         self.data = [None] * self.maxSize  #三元组表
  10.         for i in range(self.maxSize):
  11.             self.data[i] = TripleNode()
  12.         self.rows = 0 #行数
  13.         self.cols = 0 #列数
  14.         self.nums = 0 #非零元素个数

  15.     def create(self,mat):
  16.         count =k = 0
  17.         self.rows = len(mat)
  18.         self.cols = len(mat[0])
  19.         for i in range(self.rows):
  20.             for j in range(self.cols):
  21.                 if mat[i][j] !=0:
  22.                     count +=1
  23.         self.nums = count
  24.         self.data = [None] * self.nums
  25.         for i in range(self.rows):
  26.             for j in range(self.cols):
  27.                 if mat[i][j] !=0:
  28.                     self.data[k] = TripleNode(i,j,mat[i][j])
  29.                     k +=1

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

  34.     def ms_add(self,mat1,mat2):
  35.         k = count1=count2=0
  36.         self.rows = len(mat1)
  37.         self.cols = len(mat1[0])
  38.         for i in range(self.rows):
  39.             for j in range(self.cols):
  40.                 if mat1[i][j] !=0:
  41.                     count1 += 1
  42.                 if mat2[i][j] !=0:
  43.                     count2 +=1
  44.         self.nums = count1+count2
  45.         self.data = [None] * self.nums
  46.         for i in range(self.rows):
  47.             for j in range(self.cols):
  48.                 if mat1[i][j] != 0 and mat2[i][j] == 0:
  49.                     self.data[k] = TripleNode(i,j,mat1[i][j])
  50.                     k+=1
  51.                 elif mat1[i][j] == 0 and mat2[i][j] != 0:
  52.                     self.data[k] = TripleNode(i,j,mat2[i][j])
  53.                     k+=1
  54.                 elif mat1[i][j] != 0 and mat2[i][j] != 0:
  55.                     self.data[k] = TripleNode(i,j,mat1[i][j]+mat2[i][j])
  56.                     k+=1
  57.         self.nums=k





  58. M1 = [
  59.     [0,1,1],
  60.     [0,0,0],
  61.     [0,1,0]
  62. ]

  63. M2 = [
  64.     [0,1,1],
  65.     [0,0,0],
  66.     [0,1,0]
  67. ]

  68. # print(len(M1))
  69. # print(len(M1[0]))
  70. B1=SparseMatrix(100)
  71. B1.create(M1)

  72. B2=SparseMatrix(100)
  73. B2.create(M2)

  74. B3=SparseMatrix(100)
  75. B3.ms_add(M1,M2)

  76. print("矩阵A的三元组: ")
  77. B1.display()
  78. print("矩阵B的三元组: ")
  79. B2.display()
  80. print("矩阵A和矩阵B相加后的矩阵C的三元组: ")
  81. B3.display()

  82. # B1.dispaly()
  83. # B2.dispaly()
复制代码
小甲鱼最新课程 -> https://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

修正后的代码:
  1. class TripleNode(object):
  2.     def __init__(self,row=0,column=0,value=0):
  3.         self.row = row
  4.         self.column = column
  5.         self.value = value

  6. class SparseMatrix(object):
  7.     def __init__(self,maxSize):
  8.         self.maxSize = maxSize
  9.         self.data = [None] * self.maxSize  #三元组表
  10.         for i in range(self.maxSize):
  11.             self.data[i] = TripleNode()
  12.         self.rows = 0 #行数
  13.         self.cols = 0 #列数
  14.         self.nums = 0 #非零元素个数

  15.     def create(self,mat):
  16.         count =k = 0
  17.         self.rows = len(mat)
  18.         self.cols = len(mat[0])
  19.         for i in range(self.rows):
  20.             for j in range(self.cols):
  21.                 if mat[i][j] !=0:
  22.                     count +=1
  23.         self.nums = count
  24.         self.data = [None] * self.nums
  25.         for i in range(self.rows):
  26.             for j in range(self.cols):
  27.                 if mat[i][j] !=0:
  28.                     self.data[k] = TripleNode(i,j,mat[i][j])
  29.                     k +=1

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

  34.     def ms_add(self,mat1,mat2):
  35.         k = count1=count2=0
  36.         self.rows = len(mat1)
  37.         self.cols = len(mat1[0])
  38.         for i in range(self.rows):
  39.             for j in range(self.cols):
  40.                 if mat1[i][j] !=0:
  41.                     count1 += 1
  42.                 if mat2[i][j] !=0:
  43.                     count2 +=1
  44.         self.nums = count1+count2
  45.         self.data = [None] * self.nums
  46.         for i in range(self.rows):
  47.             for j in range(self.cols):
  48.                 if mat1[i][j] != 0 and mat2[i][j] == 0:
  49.                     self.data[k] = TripleNode(i,j,mat1[i][j])
  50.                     k+=1
  51.                 elif mat1[i][j] == 0 and mat2[i][j] != 0:
  52.                     self.data[k] = TripleNode(i,j,mat2[i][j])
  53.                     k+=1
  54.                 elif mat1[i][j] != 0 and mat2[i][j] != 0:
  55.                     self.data[k] = TripleNode(i,j,mat1[i][j]+mat2[i][j])
  56.                     k+=1
  57.         self.nums=k





  58. M1 = [
  59.     [0,1,1],
  60.     [0,0,0],
  61.     [0,1,0]
  62. ]

  63. M2 = [
  64.     [0,1,1],
  65.     [0,0,0],
  66.     [0,1,0]
  67. ]

  68. # print(len(M1))
  69. # print(len(M1[0]))
  70. B1=SparseMatrix(100)
  71. B1.create(M1)

  72. B2=SparseMatrix(100)
  73. B2.create(M2)

  74. B3=SparseMatrix(100)
  75. B3.ms_add(M1,M2)

  76. print("矩阵A的三元组: ")
  77. B1.display()
  78. print("矩阵B的三元组: ")
  79. B2.display()
  80. print("矩阵A和矩阵B相加后的矩阵C的三元组: ")
  81. B3.display()

  82. # B1.dispaly()
  83. # B2.dispaly()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-7 12:37:59 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 19:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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