我尝试过这个程序
from pprint import pprint
m = int(input("Number of matrices: "))
rows = int(input("Enter the Number of rows : "))
columns = int(input("Enter the Number of Columns: "))
matrices = []
for i in range(m):
print("Enter the elements of Matrix:")
matrix_i = [[tuple(map(float, input().split(" "))) for c in range(columns)]
for r in range(rows)]
print("Matrix no: ", i + 1)
for n in matrix_i:
print(n)
print()
matrices.append(matrix_i)
pprint(matrices)
def areequal(A,B):
for i in range(rows):
for j in range(columns):
if (((A[i][j][0] == B[i][j][0]), (A[i][j][1] == B[i][j][1]), (A[i][j][2] == B[i][j][2]))):
return 1
return 0
for m1 in matrices:
for m2 in matrices:
if (areequal(m1, m2)==1):
print(m1, m2)
print("Matrices are identical")
else:
print(m1, m2)
print("Matrices are not identical")
使以下矩阵的输出相同
m1=
[(1.0, 2.0, 3.0) (8.0, 7.0, 6.0)]
[(8.0, 7.0, 6.0) (4.0, 5.0, 6.0)]
m2 =
[(3.0, 4.0, 5.0) (9.0, 8.0, 7.0)]
[(3.0, 4.0, 5.0) (9.0, 8.0, 7.0)]
m3 =
[(0.0, 9.0, 7.0) (2.0, 3.0, 4.0)]
[(8.0, 7.0, 6.0) (8.0, 7.0, 6.0)]
比如这样
[[(0.0, 9.0, 7.0), (2.0, 3.0, 4.0)], [(8.0, 7.0, 6.0), (8.0, 7.0, 6.0)]] [[(3.0, 4.0, 5.0), (9.0, 8.0, 7.0)], [(3.0, 4.0, 5.0), (9.0, 8.0, 7.0)]]
Matrices are identical
[[(0.0, 9.0, 7.0), (2.0, 3.0, 4.0)], [(8.0, 7.0, 6.0), (8.0, 7.0, 6.0)]] [[(0.0, 9.0, 7.0), (2.0, 3.0, 4.0)], [(8.0, 7.0, 6.0), (8.0, 7.0, 6.0)]]
Matrices are identical
即使矩阵不相同/不等,输出也相同。
为什么我让所有矩阵都相同?如何求两个n元矩阵相等?如何找到给定的矩阵两两相等(如 (m1 & m2)、(m2 & m3)、(m3 & m1))?如何获得不重复的结果(如(m1&m1),(m2&m2),(m3&m3))?
逗号创建元组,而不是布尔值。元组是真实的。线
if (((A[i][j][0] == B[i][j][0]), (A[i][j][1] == B[i][j][1]), (A[i][j][2] == B[i][j][2]))):
本质上是
if True:
相反,使用
if (((A[i][j][0] == B[i][j][0]) and (A[i][j][1] == B[i][j][1]) and (A[i][j][2] == B[i][j][2]))):