[Solved] How to create a 3D matrix with 3 columns and each column having 1000 rows [closed]


It is still not really clear what you want.

If you want multidimensional array you can use lists:

>>> matrix = [[None]*10 for x in range(3)]#replace 10 with 1000 or what ever
>>> matrix
[[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]]
>>>

Also I would recommend the use of None rather that 0.

You can acces the matrix like this:

>>> matrix[1][3] = 55
>>> matrix
[[None, None, None, None, None, None, None, None, None, None], [None, None, None, 55, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None]]

Is this what you were aiming for?

For a better visual representation you could do something like:

>>> for x in matrix:
...     print(x, "\n")
... 
[None, None, None, None, None, None, None, None, None, None] 

[None, None, None, 55, None, None, None, None, None, None] 

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


You could also go with:

>>> matrix = [[None]*10 for x in xrange(3)]

Read about it here.

Since you are using python 3x. you should use range(). Se more here.

Oh and by the way there is nothing particularly wrong with what you are doing, you are using a tuple instead of a list, these are not mutable, but the nested lists inside are, so you can modify it:

>>> positionMatrix = ([0]*10, [0]*10, [0]*10)
>>> positionMatrix
([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> positionMatrix[0][4] = 99
>>> positionMatrix
([0, 0, 0, 0, 99, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Just don’t do this:

>>> positionMatrix = [[0]*10]*3
>>> positionMatrix[0][4] = 99
>>> positionMatrix
[[0, 0, 0, 0, 99, 0, 0, 0, 0, 0], [0, 0, 0, 0, 99, 0, 0, 0, 0, 0], [0, 0, 0, 0, 99, 0, 0, 0, 0, 0]]
>>> 

It refers to the same object in memory.

Just in case, you can use this:

>>> positionMatrix = [[0]*10, [0]*10, [0]*10]
>>> positionMatrix
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> positionMatrix[0][4] = 99
>>> positionMatrix
[[0, 0, 0, 0, 99, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

Since you would be creating 3 different objects.

6

solved How to create a 3D matrix with 3 columns and each column having 1000 rows [closed]