def list_2d_locations(rows, columns, low_range, high_range):
matrix = list_2d_generate(rows, columns, low_range, high_range)
print(matrix)
low_val = high_val = matrix[0][0]
for i in range(rows):
for j in range(columns):
if matrix[i][j] < low_val:
low_val = matrix[i][j]
low_loc = [i][j]
if matrix[i][j] > high_val:
high_val = matrix[i][j]
high_loc = [i][j]
return low_val, high_val, low_loc, high_loc
我这里有一个函数,它应该在列表列表(即矩阵)中找到最小和最大的数字并返回实际值,以及该值在矩阵中的位置。现在我的问题是两者high_loc = [i][j]
都给low_loc = [i][j]
我“列表索引超出范围”的错误,我不明白为什么。那么 if 语句是否也超出了同样的逻辑范围?