I am a bit confused about the behaviour of multi-dimensional Python "arrays" (actually lists of lists, not numpy
arrays). Suppose I have a 4 x 4 array whose entries are 1 x 2 arrays (that is, entries of the 4 x 4 array are lists containing 2 lists). I can initialize such an empty array with the command:
b = [[[[], ]*2, ]*4, ]*4
This creates the following multidimensional empty array:
[[[[], []], [[], []], [[], []], [[], []]],
[[[], []], [[], []], [[], []], [[], []]],
[[[], []], [[], []], [[], []], [[], []]],
[[[], []], [[], []], [[], []], [[], []]]]
Now I want to modify a single entry of this 4 x 4 array; for instance I want to make the component b[0][0]
to be equal to [[1],[2]]
:
[[[[1], [2]], [[], []], [[], []], [[], []]],
[[[], []], [[], []], [[], []], [[], []]],
[[[], []], [[], []], [[], []], [[], []]],
[[[], []], [[], []], [[], []], [[], []]]]
My expectation was that the following command would do that job:
b[0][0] = [[1],[2]]
However, this leads instead to the following matrix b
:
[[[[1], [2]], [[], []], [[], []], [[], []]],
[[[1], [2]], [[], []], [[], []], [[], []]],
[[[1], [2]], [[], []], [[], []], [[], []]],
[[[1], [2]], [[], []], [[], []], [[], []]]]
What is the proper way to achieve this?