如何正确索引n维数组?

时间:2019-05-11 23:43:18

标签: python arrays

我想构造这些n维的“矩阵”,然后在每个相应的位置存储一些给定的数据。不过,问题是,我已经尝试了几乎所有的索引组合,但都没有成功到达我想要的位置。以下是其中一个示例,

def distances(fstr, sstr):

    di_X = len(fstr)+1
    di_Y = len(sstr)+1
    Matrix = [[[None]*2]*(di_X)]*(di_Y)
--> [[[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]]]

然后Matrix[0][1] = 1通过以下方式对其进行更改:

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

Matrix[3][1] = 1产生完全相同的输出(依此类推):

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

我希望它将带我到第0行和第一个元素,但是它碰巧更改了每一行的第一个元素。有人可以解释其背后的原因吗?如果这恰好是该网站功能的未充分利用,我真的很抱歉,但是我相信我已经用尽了所有其他资源。

0 个答案:

没有答案