简体   繁体   English

矩阵python函数

[英]Matrix python function

I have to write a function whereby given a matrix that is non empty and not negative, a starting position (a nonnegative list of length 2 that represents the position of the matrix) shorthand for the matrix[i][j] , it will return the position of the lowest adjacent value (keeps going until it finds the lowest only going through its adjacent cells/elements). 我有由此给出一个矩阵,该矩阵是一个非空和非负,对于一个起始位置(即表示矩阵的位置长度为2的非负列表)速记写一个函数matrix[i][j]它会返回最低相邻值的位置(保持吉普车前进,直到找到仅通过其相邻单元格/元素的最低值)。

example: if matrix = [[8,90,91,73],[60,6,32,84],[50,4,45,94],[12,85,3,2]] and sarting position = [0,0] , it should return [3,3] . 例如:如果matrix = [[8,90,91,73],[60,6,32,84],[50,4,45,94],[12,85,3,2]]且销售位置= [0,0] ,它应该返回[3,3] I want to try to implement this function with only basics of programming (if statements, loops etc.) simple helper functions like min or max can be used but nothing too advanced please. 我想尝试仅使用编程的基础知识(如果语句,循环等)来实现此功能,可以使用诸如min或max之类的简单辅助函数,但请不要太高级。

This is what I have so far but it doesn't really work nicely as I have the index out or range errors: 到目前为止,这是我所拥有的,但是由于索引超出范围或范围错误,它实际上并不能很好地工作:

def search_local_lowest_value(s: List[List[int]], position: List[int]) -> List[int]:
element1 = position[0]
element2 = position[1]
local_variable = s[element1][element2]

while local_variable <= s[row + 1][column] or local_variable <= s[row + 1][column + 1] \
    or local_variable <= s[row + 1][column - 1] or local_variable <= s[row][column + 1] \
    or local_variable <= s[row][column - 1] or local_variable <= s[row -1][column] \
    or local_variable <= s[row - 1][column + 1] or local_varaible <= s[row - 1][column - 1]:
        if local variable <= s[row + 1][column] or local_variable <= s[row + 1][column + 1] \
        or local_variable <= s[row + 1][column - 1] or local_variable <= s[row][column + 1] \
        or local_variable <= s[row][column - 1] or local_variable <= s[row -1][column] \
        or local_variable <= s[row - 1][column + 1] or local_variable <= s[row - 1][column - 1]:
            return min() // don't know what to fill for that
        else: 
            return False

I came out with this solution, I used methods to be more clear and dry: 我提出了这个解决方案,我使用的方法更加清晰干燥:

def bordering(start, shape):
  PATTERN = [[-1,-1], [-1, 0], [-1, 1], [0, 0], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
  border = [[start[0] + x, start[1] + y] for x, y in PATTERN]
  valid_border = [item for item in border if item[0] >= 0 and item[1] >= 0 and item[0] <= shape[0] - 1   and item[1] <= shape[1] - 1  ] # removes borders out of the matrix boundaries
  return valid_border

def smaller_from(start, matrix):
  shape = [len(matrix), len(matrix[0])]
  borders = bordering(start, shape)
  minimum = min([ [matrix[x][y], [x,y]] for x,y in borders ]) # maps the values and coordinates in valid borders and returns the minimum
  return minimum

def find_minimum(start, matrix):
  result = []
  while True:
    val_coords = smaller_from(start, matrix)
    result = val_coords
    if val_coords[0] >= matrix[start[0]][start[1]]:
      break
    else:
      start = val_coords[1]
  return result

matrix = [
          [8,90,91,73],
          [60,6,32,84],
          [50,4,45,94],
          [12,85,3,2]]
start = [0, 0]

find_minimum(start, matrix) #=> [2, [3, 3]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM