Algorithm 将矩阵分解为更小的子列表

Algorithm 将矩阵分解为更小的子列表,algorithm,Algorithm,我不知道怎样才能做到这一点。更具体地说,我想打破以下矩阵 matrix = [[7, 9, 1, 8, 9, 1], [4, 2, 1, 2, 1, 5], [3, 2, 3, 1, 2, 3], [7, 9, 11, 6, 4, 8], [8, 9, 22, 3, 1, 9], [1, 1, 1, 1, 1, 1]] 进入: 或同等地 以下是我尝试过的: def split([[]]) -> [[

我不知道怎样才能做到这一点。更具体地说,我想打破以下矩阵

matrix = [[7, 9, 1, 8, 9, 1],
         [4, 2, 1, 2, 1, 5],
         [3, 2, 3, 1, 2, 3],
         [7, 9, 11, 6, 4, 8],
         [8, 9, 22, 3, 1, 9],
         [1, 1, 1, 1, 1, 1]]
进入:

或同等地

以下是我尝试过的:

def split([[]]) -> [[]]
  split_matrix = []
  temp_map = []
  row_limit, col_limit = 2, 2

  for row in range(len(elevation_map)):
      for col in range(len(elevation_map)):
            elevation = elevation_map[row][col]
          if row < row_limit and col < col_limit:
              temp_map.append(elevation)
  split_matrix.append(temp_map)
  return split_matrix
def拆分([[]])->[[]]
分割矩阵=[]
温度映射=[]
行限制,列限制=2,2
对于范围内的行(len(立面图)):
对于范围内的col(len(立面图)):
高程=高程\地图[行][列]
如果行<行\u限制和列<列\u限制:
临时地图附加(立面图)
拆分矩阵追加(临时映射)
返回分裂矩阵
然而,我没有运气这样做


有没有一种方法不使用numpy之类的库就可以做到这一点?有可能吗?

如果我们编写一个helper函数将一个2x2子矩阵提取到一个列表中,那么解决方案会更整洁。之后,它是一个简单的列表理解,迭代每个子矩阵左上角的坐标

def split_矩阵(矩阵,行=2,列=2):
def助手(i,j):
out=[]
对于矩阵中的行[i:i+行]:
out.extend(行[j:j+cols])
返回
宽度,高度=len(矩阵[0]),len(矩阵)
返回[
助手(i,j)
对于范围内的i(0,高度,行)
对于范围内的j(0,宽度,cols)
]
[[7, 9, 4, 2],
[1, 8, 1, 2],
[9, 1, 1, 5],
[3, 2, 7, 9],
[3, 1, 11, 6],
[2, 3, 4, 8],
[8, 9, 1, 1],
[22, 3, 1, 1],
[1, 9, 1, 1]]
def split([[]]) -> [[]]
  split_matrix = []
  temp_map = []
  row_limit, col_limit = 2, 2

  for row in range(len(elevation_map)):
      for col in range(len(elevation_map)):
            elevation = elevation_map[row][col]
          if row < row_limit and col < col_limit:
              temp_map.append(elevation)
  split_matrix.append(temp_map)
  return split_matrix