1

就在我认为自己擅长某事的时候,我发现了一件我无法克服的简单事情。

我需要创建一个对称的行 x 列矩阵,给定一个校验和的开始和块。条目应按顺序排列。

def main(start_index, block):
    num_rows, num_cols = block, block
    matrix = []

    for r in range(num_rows):
        temp = []

        for c in range(num_cols):
            temp.append(c)

        matrix.append(temp)

    return matrix

这里的输出是: [[0, 1, 2], [0, 1, 2], [0, 1, 2]]

我想要获得的是: [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

不仅适用于 3x3,还适用于动态。

注意:没有像 numpy 这样的包,这不是这个问题的重点。只有本机python。

4

3 回答 3

1

您要附加c的始终是 0 和num_cols. 您需要c根据您所在的行进行计算。例如:

def main(start_index, block):
    num_rows, num_cols = block, block
    matrix = []

    for r in range(num_rows):
        temp = []

        for c in range(0, num_cols):
            temp.append(start_index + c + (r * num_cols))

        matrix.append(temp)

    return matrix

main(0, 3)

>> [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

你也可以把它写成一个生成器 from start_indextorow * columns懒惰地生成你的行。例如:

def main(start_index, block):
    num_rows, num_cols = block, block

    total = num_rows * num_cols

    for i in range(start_index, total + start_index, num_cols):
        yield list(range(i, num_cols + i))

list(main(10, 4))
>> [[10, 11, 12, 13], [14, 15, 16, 17], [18, 19, 20, 21], [22, 23, 24, 25]]
于 2019-12-29T07:31:17.543 回答
1

你是不是太复杂了?

In [1]: nrow, ncol, start = 3, 5, 7                                                       

In [2]: [[start+c+r*ncol for c in range(ncol)] for r in range(nrow)]                      
Out[2]: [[7, 8, 9, 10, 11], [12, 13, 14, 15, 16], [17, 18, 19, 20, 21]]

如果您更喜欢描述性更强的名称:

In [3]: n_rows, n_cols, n_start = 3,5,7                        

In [4]: [[n_start + col + row*n_cols for col in range(n_cols)] for row in range(n_rows)]               
Out[4]: [[7, 8, 9, 10, 11], [12, 13, 14, 15, 16], [17, 18, 19, 20, 21]]

最终,拥有itertools.count绝对不需要import itertools

In [10]: def count(start=0, step=1): 
    ...:     while True: 
    ...:         yield start 
    ...:         start += step 

In [11]: nr, nc, item = 3, 4, count()                                                                                                                                              

In [12]: [[next(item) for c in range(nc)] for r in range(nr)]                                                                                                                      
Out[12]: [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]

In [13]: nr, nc, item = 3, 4, count(start=4, step=3)                                                                                                                               

In [14]: [[next(item) for c in range(nc)] for r in range(nr)]                                                                                                                      
Out[14]: [[4, 7, 10, 13], [16, 19, 22, 25], [28, 31, 34, 37]]
于 2019-12-29T07:51:24.940 回答
0

您可以使用itertools.count创建一个生成器来生成递增整数,每次调用next都会获取下一个整数

然后,您可以在创建列表时使用此生成器填写值

import itertools

def get_square_matrix(size):
    counter = itertools.count(start_index)
    matrix = []
    for row in range(size):
        matrix.append([next(counter) for _ in range(size)])
    return matrix
于 2019-12-29T07:28:11.327 回答