Create an empty Python matrix

I just want to create an empty 10 * 3 * 2 array with Python.

At first I thought about it, but this does not work:

parameters = [ [ [] * 2 ]*3 ] * 10 

this gives me a vector of ten vectors with three [] elements in it:

 [[[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []]] 

that is, if I want to access the parameters [0] [0] [1], I went beyond the limits, whereas I need size 2 for the innermost vectors along the third dimension.

then I thought about it

 [ [ [[] * 2] ]*3 ] * 10 

I thought that [[] * 2] would now bring me what I want, the innermost vector of two elements. I get

 [[[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]]] 

So how to do this or how to avoid this initialization?

Kd rgds.

+6
source share
4 answers

I would recommend you use Numpy for this kind of thing. This simplifies access to columns or rows. For your use case you would do

 import numpy as np matrix = np.zeros((2,3,10)) second_col = matrix[:,1,:] 

Numpy will also take better care of your data, and it implements a lot of matrix algebra in Fortran or C, so in the future (possible) it will be much faster when you do matrix multiplication and like it.

+17
source

First of all, you should insert something in the innermost list (e.g. None). Secondly, when you use multiplication in an external list, it replicates the links in the internal list, so when you change one element, you will also change this element in all other lists:

 >> parameters = [ [ [None] * 2 ]*3 ] * 10 >> print parameters [[[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]]] >> parameters[0][0][1]=1 >> print parameters [[[None, 1], [None, 1], [None, 1]], [[None, 1], [None, 1], [None, 1]], [[None, 1], [None, 1], [None, 1]], [[None, 1], [None, 1], [None, 1]], [[None, 1], [None, 1], [None, 1]], [[None, 1], [None, 1], [None, 1]], [[None, 1], [None, 1], [None, 1]], [[None, 1], [None, 1], [None, 1]], [[None, 1], [None, 1], [None, 1]], [[None, 1], [None, 1], [None, 1]]] 

Therefore, you are better off using lists:

 >> parameters=[[[None for i in range(2)] for j in range(3)] for k in range(10)] 

However, I would recommend using numpy , as suggested in one of the other answers.

+11
source

I would do something like this using these created lists - these are different objects (i.e. different id() ):

 In [96]: [ [ [ []*2] for _ in range(3)] for _ in range(10) ] Out[96]: [[[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]]] In [98]: [id(x) for x in lis] #all objects are unique Out[98]: [151267948, 151268076, 151268492, 151269164, 151267276, 151265356, 151268140, 151269036, 151265644, 151265964] In [101]: lis1=[ [ [[] * 2] ]*3 ] * 10 In [102]: [id(x) for x in lis1] # all objects are same, changing one will change # others as well Out[102]: [151278188, 151278188, 151278188, 151278188, 151278188, 151278188, 151278188, 151278188, 151278188, 151278188] 
+6
source

Here is one of the problems with what you do.

Let's say you create an array, for example:

 >>> l = [ [ [[] * 2] ]*3 ] * 10 >>> l [[[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]]] 

Everything seems to be in order. Let something be given in the array.

 >>> l[0][0][0] = 2 >>> l [[[2], [2], [2]], [[2], [2], [2]], [[2], [2], [2]], [[2], [2], [2]], [[2], [2], [2]], [[2], [2], [2]], [[2], [2], [2]], [[2], [2], [2]], [[2], [2], [2]], [[2], [2], [2]]] 

Wow! We installed 1 element in it, but it changed everything! How did this happen?

Well, it looks like we have 60 list objects. However, in fact, we have 60 links to one list object. Change it, change everything.

TL DR: Do not use the multiplication operator in a list of lists.

+4
source

All Articles