In Python there is a special symbol "*" that can be used as a multiplication operator for numeric operations and as a repetition operator for objects, but must be noted when used as a repeating operator
Note: * Repeated objects have the same ID, that is, pointing to the same address in memory, it is important to note that each object is manipulated.
For example:
>>> alist = [Range (3)]*4>>> alist[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]
The above initializes a two-level list to simulate the matrix, the Matrix 4x3, for the convenience of description, here the matrix is a.
Now I want to assign a value of 1 to A11, using the following code:
1
Alist[0][0]=1
So the result we want is:
1
[[1, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]
But unfortunately, what we get is:
1
[[1, 1, 2], [1, 1, 2], [1, 1, 2], [1, 1, 2]]
What is this, why give A21, why the other Ai1 have changed?
The reason is this:
At the beginning of the article we have said, * repeated objects have the same ID, that is, pointing to the same address in memory, it is important to note the operation of the individual objects.
When we initialize again, we use the repetition operator "*", which, when repeating the object, points all the duplicates to the same memory address, all when you change one of these values.
Other values are naturally updated, and the following commands and outputs are explained in Python:
>>> ID (alist[0]) 18858192>>> ID (alist[1]) 18858192>>> ID (alist[2]) 18858192>>> ID (Alist[3]) 18858192>>>
See, the IDs are all the same, which means the 4 lists are the same "list".
In that case, what do we want to do with a matrix, in addition to the special NumPy package, you can certainly give the upper list a append new list, for example:
>>> blist=[]>>> for I in range (4): blist.append ([J to J in Range (3)]) >>> blist[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]
So, let's try the assignment above:
>>> blist[0][0]=1>>> blist[[1, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]>>>