以下是比较矩阵的行的一个示例代码解决方法:
def compare_rows(matrix):
rows = len(matrix)
cols = len(matrix[0])
for i in range(rows):
for j in range(i+1, rows):
if matrix[i] == matrix[j]:
print("Row", i+1, "and Row", j+1, "are equal.")
else:
print("Row", i+1, "and Row", j+1, "are not equal.")
# 测试
matrix = [[1, 2, 3], [4, 5, 6], [1, 2, 3], [7, 8, 9]]
compare_rows(matrix)
输出结果:
Row 1 and Row 2 are not equal.
Row 1 and Row 3 are equal.
Row 1 and Row 4 are not equal.
Row 2 and Row 3 are not equal.
Row 2 and Row 4 are not equal.
Row 3 and Row 4 are not equal.
以上代码中,我们定义了一个compare_rows
函数,它接受一个矩阵作为参数。函数首先获取矩阵的行数和列数,然后使用两个嵌套的循环来比较矩阵的每一行。
在循环中,我们使用索引i
和j
来迭代矩阵的每一行。我们通过比较matrix[i]
和matrix[j]
来判断两行是否相等。如果相等,我们打印一条相等的消息。如果不相等,我们打印一条不相等的消息。
在示例中,我们使用一个包含四行三列的矩阵进行测试。输出结果显示了每一行与其他行的比较结果。