This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 years ago.
I have trouble declaring a two-dimensional list in Python. Below are two different matrices; A and R. When changing the content of a single cell I am successful in the R-matrix, but not so much in the A-matrix, as the value-input affect the entire column and not only the single cell.
Why does this happen? I would prefer the A-style of declaring the matrix.
n=6
A = [[0]*n]*n
R=[[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], [0,0,0,0,0,0]]
R[1][1]=5
A[1][1]=5
print(R)
print(A)
The output from the two operations is:
[[0, 0, 0, 0, 0, 0], [0, 5, 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, 5, 0, 0, 0, 0], [0, 5, 0, 0, 0, 0], [0, 5, 0, 0, 0, 0], [0, 5, 0, 0, 0, 0], [0, 5, 0, 0, 0, 0], [0, 5, 0, 0, 0, 0]]
A = [[0]*n]*n creates multiple copies of the same list. This is why changing one affects every other one
Related
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 years ago.
Here's a 3*4 matrix represented by embedded list, when I try to assign the dp[0][0][0] = 1, it changes all first value of each list element.
I'm using python 3.7, don't know where's the problem.
I want to change the first value of first [0,0,0,0] to 4
dp = [[[0,0,0,0]] * 3] *4
dp[0][0][0] =4
print(dp)
output like this
[[[4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0]], [[4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0]], [[4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0]], [[4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0]]]
You do it like this :
dp = [[[0,0,0,0] for y in range(3)] for i in range(4)]
dp[0][0][0] =4
print(dp)
Output:
[[[4, 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, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
Have a good day !
a = np.diag(np.array([2,3,4,5,6]),k=-1)
For the above code, I want to know how to change it for shaping the 6*6 matrix into 6*5 matrix with the first line is filled with 0 and the following lines with 2,3,4,5,6 to be diagonal? Thank you very much
I don't understand what you want to know.
In your code if k>0
then the resultant matrix will have k extra columns,if k=2 then,
output will be :
array([[0, 0, 2, 0, 0, 0, 0],
[0, 0, 0, 3, 0, 0, 0],
[0, 0, 0, 0, 4, 0, 0],
[0, 0, 0, 0, 0, 5, 0],
[0, 0, 0, 0, 0, 0, 6],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
And if k<0 then it will have the k extra rows , for example if k=-1
then:
array([[0, 0, 0, 0, 0, 0],
[2, 0, 0, 0, 0, 0],
[0, 3, 0, 0, 0, 0],
[0, 0, 4, 0, 0, 0],
[0, 0, 0, 5, 0, 0],
[0, 0, 0, 0, 6, 0]])
and if k=0 then :
array([[2, 0, 0, 0, 0],
[0, 3, 0, 0, 0],
[0, 0, 4, 0, 0],
[0, 0, 0, 5, 0],
[0, 0, 0, 0, 6]])
I think you want to create a matrix of 5*5 and then want too add a row. Then you can do it using this
a=a.tolist()
Now a is 2d list and you can insert the row wherever you want.
Do this for your result.
a.insert(0,[0,0,0,0,0])
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 7 years ago.
s is a list of lists of integers with all values initialized to zero. I would like to increment only the first element of the first row by one, but the following command increments the first element of every row by one. How may I achieve this?
In [6]: s = [[0]*4]*4
In [7]: s[0][0] += 1
In [8]: s
Out[8]:
[[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0]]
Okay! Thanks for the advice, the problem was in my construction of s.
If s is truly a list of lists (and not a list containing multiple references to the same list), what you did works, your issue must be elsewhere
>>> s = [[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]]
>>> s[0][0]
0
>>> s[0][0] = 1
>>> s
[[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
You made your lists "incorrectly" in the first place; each element of your list simply points to the same single list. When you update that list they all update.
Make the list of lists using code something like this instead:
s = [[0 for _ in range(5)] for _ in range(5)]
This is classical Python oversight since lists assignments are done by references not by deep copy.
For example if you constructed using this way that's where it would have gone wrong.
>>> zeros = [0,0,0,0]
>>> s = [zeros,zeros,zeros,zeros]
>>> s[0][0]+=1
>>> s
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
So while copying lists use as below
>>> s = [list(zeros), list(zeros), list(zeros), list(zeros)]
>>> s[0][0]+=1
>>> s
[[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
This question already has answers here:
How do I get the number of elements in a list (length of a list) in Python?
(11 answers)
Closed 7 years ago.
I have a list like this :
test=[[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1],
[0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1],
[0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1],
[1, 0, 0, 0], [1, 0, 0, 0],[0,0,0,0]]
How can i count how many lists i have inside list called test ?
You will utilize the len function for this task:
Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
>>> test=[[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1],
... [0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1],
... [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1],
... [1, 0, 0, 0], [1, 0, 0, 0],[0,0,0,0]]
>>> len(test)
18
Initializing a matrix as so seems to link the rows so that when one row changes, they all change:
>>> grid = [[0]*5]*5
>>> grid
[[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]]
>>> grid[2][2] = 1
>>> grid
[[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0]]
How can I avoid this?
grid = [[0]*5 for i in range(5)]
Note: [int]*5 copies the int 5 times (but when you copy an int you just copy the value). [list]*5 copies the reference to the same list 5 times. (when you copy a list you copy the reference that points to the list in memory).