我有一个要更改的矩阵。有一个开始和停止位置,并且开始和停止之间的每个值都会被某些因素改变。问题是我无法找出如何仅更改可定制范围内的某些值。开始和停止位置作为列表[行,列]输入。
我已包含我的代码。为此,我不断收到错误:TypeError:'int'和'list'的实例之间不支持'> ='(因为我试图将矩阵的位置与开始和停止的位置进行比较。
def change_matrix(matrix: List[List[int]], start_pos: List[int],
stop_pos: List[int], delta: int) -> None:
a = int(start_pos[0]) #tried taking the int of slice to avoid error
b = start_pos[1]
c = stop_pos[0]
d = stop_pos[1]
print(a, b, c, d)
for row in range(len(matrix)):
for elevation in range(len(matrix[row])):
if row >= matrix[a]: #get error here
if row <= matrix[b]:
if elevation >= matrix[c]:
if elevation >= matrix[d] >= a:
update = matrix[row][elevation] + delta
matrix.remove(matrix[row][elevation])
matrix.insert(update)
我觉得您的逻辑过于复杂。你可以试试吗?
matrix = [[1,2,3],[4,5,6]]
def change_matrix(matrix, start_pos, stop_pos, delta):
i = start_pos[0]
while i <= start_pos[1]:
j = stop_pos[0]
while j <= stop_pos[1]:
matrix[i][j] += delta
j += 1
i += 1
return matrix
print(change_matrix(matrix, [0,1], [0,1], 1))