0

我正在尝试制作一个将二进制列表交错到数组中的函数。矩阵大小为 10 行 x 9 列,初始化为None. 这些位一次被送入矩阵,第一个输入位被送入 (0,0)。这些位被送入(前一行 + 7)模 10,当列满时,列向右移动,第 0 列填充下一组位。但是,我遇到了列填充循环的问题。零列中的每一行都被设置为相同的值!

这是我到目前为止所拥有的:

>>> row = 0
>>> interleave_row = [None,None,None,None,None,None,None,None,None]
>>> interleave_matrix = [interleave_row,interleave_row,interleave_row,interleave_row,interleave_row,interleave_row,interleave_row,interleave_row,interleave_row,interleave_row]

这会产生一个矩阵,如下所示:

    [None, None, None, None, None, None, None, None, None],
    [None, None, None, None, None, None, None, None, None],
    [None, None, None, None, None, None, None, None, None],
    [None, None, None, None, None, None, None, None, None],
    [None, None, None, None, None, None, None, None, None],
    [None, None, None, None, None, None, None, None, None],
    [None, None, None, None, None, None, None, None, None],
    [None, None, None, None, None, None, None, None, None],
    [None, None, None, None, None, None, None, None, None]]
>>> input = [0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0]
>>> for i in range(0,10):
        interleave_matrix[row][0] = input[i]
        row = (row+7)%10

这产生:

    [1, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None]]

应该如下所示:

    [0, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None],
    [0, None, None, None, None, None, None, None, None],
    [0, None, None, None, None, None, None, None, None],
    [0, None, None, None, None, None, None, None, None],
    [0, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None],
    [1, None, None, None, None, None, None, None, None]]

我有一个菜鸟时刻吗?我知道行增量工作正常,因为我自己循环了它。什么可能导致这种奇怪的行为?

编辑:我只是尝试循环遍历前四位(保证以 0 位结束),它没有将整行设置为该位,它只是将整行设置为 1。越来越奇怪了……

4

1 回答 1

0

帕维尔发布的答案!

您的矩阵实际上包含对同一列的 N 个引用。因此,当您修改一列中的值时,该值也会在所有其他列中被修改。考虑以另一种方式创建矩阵以制作独立列:stackoverflow.com/q/2397141/6131611 stackoverflow.com/q/6667201/6131611

这绝对是一个菜鸟时刻,但我必须从中学习!谢谢!

于 2020-05-02T19:43:15.730 回答