I have a list of lists of numpy arrays. I would like to loop through the list of lists and change certain elements. Below is an example, but just with integers:
a = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
print(a)
for i in range(len(a)):
for j in range(i+1):
if i == j:
a[i][j] = 100
print(a)
This is the output:[[1, 1, 1], [1, 1, 1], [1, 1, 1]][[100, 1, 1], [1, 100, 1], [1, 1, 100]]
The i==j element has been changed to something else. When I run the exact same code logic, but with 2x2 numpy arrays, I cannot get it to do the same thing:
L=3
I = np.eye(2, dtype = int)
sigplus = np.array([[0,2],[0,0]])
plusoperatorlist=[[I]*L]*L
print(plusoperatorlist)
for i in range(len(plusoperatorlist)):
for j in range(i+1):
if i == j:
plusoperatorlist[i][j] = sigplus
print(plusoperatorlist)
This is the output:[[array([[1, 0],[0, 1]]), array([[1, 0],[0, 1]]), array([[1, 0],[0, 1]])], [array([[1, 0],[0, 1]]), array([[1, 0],[0, 1]]), array([[1, 0],[0, 1]])], [array([[1, 0],[0, 1]]), array([[1, 0],[0, 1]]), array([[1, 0],[0, 1]])]][[array([[0, 2],[0, 0]]), array([[0, 2],[0, 0]]), array([[0, 2],[0, 0]])], [array([[0, 2],[0, 0]]), array([[0, 2],[0, 0]]), array([[0, 2],[0, 0]])], [array([[0, 2],[0, 0]]), array([[0, 2],[0, 0]]), array([[0, 2],[0, 0]])]]
It changes every element to the matrix sigplus that I defined. The desired output is to still have the list of lists of identity matrices, but have the sigplus matrix at the i==j position in the list of lists. Help is much appreciated :)
I have outlined what I tried above.