优化下面代码class SparseMatrix: def __init__(self, row, col, num): self.row = row self.col = col self.num = num self.data = [] for i in range(num): self.data.append((0, 0, 0)) def set_value(self, i, j, value): if i < 0 or i >= self.row or j < 0 or j >= self.col: return False k = 0 while k < self.num and self.data[k][0] < i: k += 1 while k < self.num and self.data[k][0] == i and self.data[k][1] < j: k += 1 if k < self.num and self.data[k][0] == i and self.data[k][1] == j: self.data[k] = (i, j, value) else: self.data.insert(k, (i, j, value)) self.num += 1 def add(self, other): if self.row != other.row or self.col != other.col: return None i = j = k = 0 result = SparseMatrix(self.row, self.col, 0) while i < self.num and j < other.num: if self.data[i][0] < other.data[j][0] or ( self.data[i][0] == other.data[j][0] and self.data[i][1] < other.data[j][1]): result.set_value(self.data[i][0], self.data[i][1], self.data[i][2]) i += 1 elif self.data[i][0] == other.data[j][0] and self.data[i][1] == other.data[j][1]: result.set_value(self.data[i][0], self.data[i][1], self.data[i][2] + other.data[j][2]) i += 1 j += 1 else: result.set_value(other.data[j][0], other.data[j][1], other.data[j][2]) j += 1 while i < self.num: result.set_value(self.data[i][0], self.data[i][1], self.data[i][2]) i += 1 while j < other.num: result.set_value(other.data[j][0], other.data[j][1], other.data[j][2]) j += 1 return result A = SparseMatrix(3, 3, 2) A.set_value(0, 0, 1) A.set_value(1, 1, 2) B = SparseMatrix(3, 3, 2) B.set_value(0, 0, 2) B.set_value(1, 1, 3) # 计算 A+B C = A.add(B) # 输出结果 print("A:") for i in range(A.row): for j in range(A.col): print(A.data[i*A.col+j][2], end=" ") print() print("B:") for i in range(B.row): for j in range(B.col): print(B.data[i*B.col+j][2], end=" ") print() print("C:") for i in range(C.row): for j in range(C.col): print(C.data[i*C.col+j][2], end=" ") print()
时间: 2024-02-26 20:51:35 浏览: 102
这段代码实现了稀疏矩阵的加法,但可以进行一些优化:
1. 在 set_value 函数中,可以使用 bisect 模块的 bisect_left 函数代替 while 循环来找到插入位置,从而提高效率。
2. 在 add 函数中,使用 while 循环遍历 self.data 和 other.data 的过程可以合并为一个 while 循环,从而简化代码。同时,可以使用 += 运算符代替 result.num += 1。
3. 在输出结果时,可以直接使用 self.data[i*self.col+j][2] 来代替 C.data[i*C.col+j][2]。
下面是优化后的代码:
```python
import bisect
class SparseMatrix:
def __init__(self, row, col, num):
self.row = row
self.col = col
self.num = num
self.data = [(0, 0, 0)] * num
def set_value(self, i, j, value):
if i < 0 or i >= self.row or j < 0 or j >= self.col:
return False
k = bisect.bisect_left(self.data, (i, j, 0))
if k < self.num and self.data[k][0] == i and self.data[k][1] == j:
self.data[k] = (i, j, value)
else:
self.data.insert(k, (i, j, value))
self.num += 1
def add(self, other):
if self.row != other.row or self.col != other.col:
return None
i = j = 0
result = SparseMatrix(self.row, self.col, 0)
while i < self.num or j < other.num:
if i < self.num and (j >= other.num or self.data[i] < other.data[j]):
result.set_value(*self.data[i])
i += 1
elif j < other.num and (i >= self.num or self.data[i] > other.data[j]):
result.set_value(*other.data[j])
j += 1
else:
result.set_value(self.data[i][0], self.data[i][1], self.data[i][2] + other.data[j][2])
i += 1
j += 1
return result
A = SparseMatrix(3, 3, 2)
A.set_value(0, 0, 1)
A.set_value(1, 1, 2)
B = SparseMatrix(3, 3, 2)
B.set_value(0, 0, 2)
B.set_value(1, 1, 3)
# 计算 A+B
C = A.add(B)
# 输出结果
print("A:")
for i in range(A.row):
for j in range(A.col):
print(A.data[i*A.col+j][2], end=" ")
print()
print("B:")
for i in range(B.row):
for j in range(B.col):
print(B.data[i*B.col+j][2], end=" ")
print()
print("C:")
for i in range(C.row):
for j in range(C.col):
print(C.data[i*C.col+j][2], end=" ")
print()
```
阅读全文