首页 > 解决方案 > 将矩阵拆分为多个矩阵

问题描述

我有如下代码,我必须将矩阵拆分为 n 个子矩阵。

two_dim_array = [['. An algorithm is a set of unambiguous instructions that a mechanical computer can execute.'],
                 ['. An algorithm is a set of unambiguous instructions that a mechanical computer can execute.'], 
                 ['[b]'], 
                 ['\xa0A complex algorithm is often built on top of other, simpler, algorithms. A simple example of an algorithm is the following (optimal for first player) recipe for play at\xa0'],
                 ['tic-tac-toe'], [':'], ['[62]'], ['If someone has a "threat" (that is, two in a row), take the remaining square. Otherwise,'],
                 ['if'], [' a move "forks" to create two threats at once, play that move. Otherwise,'], ['take'],
                 [' the center square if it is free. Otherwise,'], ['if'],
                 [' your opponent has played in a corner, take the opposite corner. Otherwise,'], ['take'],
                 [' an empty corner if one exists. Otherwise,'], ['take'], [' any empty square.'],
                 ['Many AI algorithms are capable of learning from data; they can enhance themselves by learning new\xa0'],
                 ['heuristics'],
                 ['\xa0(strategies, or "rules of thumb", that have worked well in the past), or can themselves write other algorithms. Some of the "learners" described below, including Bayesian networks, decision trees, and nearest-neighbor, could theoretically, (given infinite data, time, and memory) learn to approximate any\xa0'], 
                 ['function'], [', including which combination of mathematical functions would best describe the world'],
                 ['['], ['citation needed'], [']'], ['. These learners could therefore, derive all possible knowledge, by considering every possible hypothesis and matching them against the data. In practice, it is almost never possible to consider every possibility, because of the phenomenon of "'],
                 ['combinatorial explosion'], ['", where the amount of time needed to solve a problem grows exponentially. Much of AI research involves figuring out how to identify and avoid considering broad range of possibilities that are unlikely to be beneficial.'],
                 ['[63]'], ['[64]'], ['\xa0For example, when viewing a map and looking for the shortest driving route from\xa0'],
                 ['Denver'], ['\xa0to\xa0'], ['New York'], ['\xa0in the East, one can in most cases skip looking at any path through\xa0'], ['San Francisco'], 
                 ['\xa0or other areas far to the West; thus, an AI wielding a pathfinding algorithm like\xa0'], ['A*']]

n = int(len(two_dim_array)/2)
print(n)
sub_mat = [lines for i in range(0, len(two_dim_array),n) for lines in two_dim_array[i:n] ]
print("Changed array:",sub_mat)

在这里,我试图分成两半,我只得到矩阵的前半部分。可以帮助打破这个矩阵。

标签: pythonpython-3.xmatrixsplit

解决方案


普通切片

n = len(two_dim_array)//2
one, two = two_dim_array[:n], two_dim_array[n:]

推荐阅读