Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将矩阵分解为多个矩阵_Python_Python 3.x_Matrix_Split - Fatal编程技术网

Python 将矩阵分解为多个矩阵

Python 将矩阵分解为多个矩阵,python,python-3.x,matrix,split,Python,Python 3.x,Matrix,Split,我有下面这样的代码,我必须把矩阵分成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.']

我有下面这样的代码,我必须把矩阵分成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)

在这里,我试图分成两半,我只得到矩阵的前半部分。有人能帮忙打破这个矩阵吗

您可以编写拆分函数:

def split(array,n):
    length = len(array)
    each = length//n
    m = length % n
    final = []
    begin, end = 0,0
    for i in range(n):
        end +=each
        if i<m: end+=1
        final.append(array[begin:end])
        begin = [end][0]
    return final
[len(i) for i in split(two_dim_array,1)]
[39]

[len(i) for i in split(two_dim_array,2)]
[20, 19]

[len(i) for i in split(two_dim_array,3)]
[13, 13, 13]

[len(i) for i in split(two_dim_array,4)]
[10, 10, 10, 9]

[len(i) for i in split(two_dim_array,5)]
[8, 8, 8, 8, 7]

[len(i) for i in split(two_dim_array,6)]
[7, 7, 7, 6, 6, 6]

[len(i) for i in split(two_dim_array,10)]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 3]
从结果中,我们可以看到阵列被均匀地分割

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

[a,b,c,d]->[a,b][c,d]或[a,b,c,d]->[a,c][b,d]?[a,b,c]->?类似的东西[a,b,c,d,e]->[a,b][c,d,e]