我需要编写一个自己乘以矩阵(nXn)的程序。 我必须在过程中计算最大值和最小值,而不是最终。
这是我的代码:
import csv
import time
start= time.time()
matrix = []
with open('matrix.csv', newline='') as csvfile:
exc1 = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in exc1:
matrix.append(row)
matrix_length=len(matrix)
min = 9999999999999999999999999999999999999999999
max = 0
result = [[None]*matrix_length for _ in range(matrix_length)]
for i in range(matrix_length):
for j in range(matrix_length):
for k in range(0,matrix_length):
result[i][j] += int(matrix[i][k]) * int(matrix[k][j])
if (int(result[i][j])<min):
min = int(result[i][j])
if (int(result[i][j])>max):
max = int(result[i][j])
done=time.time()
print("max:",max)
print("min:",min)
print(done-start)
这是错误:
File "<ipython-input-15-a8472dbf81c8>", line 24, in <module>
result[i][j] += int(matrix[i][k]) * int(matrix[k][j])
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'int'
你明白这是什么问题吗?
答案 0 :(得分:0)
您正在使用None初始化结果矩阵。试试这个:
result = [[0]*matrix_length for _ in range(matrix_length)]