What makes the value of B being changed!


#1
A=[[1,2,2],[5,3,2],[5,4,1]]
def M(m,n,A):
    Mij=A
    for i in range(len(A)):
        Mij[i][n:n+1]=[]
    Mij[m:m+1]=[]
    return Mij

def det2(A):
    return A[0][0]*A[1][1]-A[0][1]*A[1][0]
def det(n,A):
    d=0
    B=[[1,4,5,6],[5,1,5,5],[1,2,3,1],[1,5,5,2]]
    for i in range(3):
        d=M(0,0,B)
    return B, d

And the output is:

>>> det(3,A)
([[2]], [[2]])

Now, what makes the value of B being changed to be equal to d?


#2

what is the function and suppose to do? There are no logic variable names, no comments on what any of the code should do. Please be a little more descriptive then just a code dump

ignoring the fact that many of your variables do not follow python naming recommendations.


#3

Just look at what you’re doing to it, if you send B into a function, look at what the function does with it


#4

Dude I’ve just start learning to code in this week.
I’m trying to make a code that calculates the determinant of an n*n matrix, and I made this:

A=[[1,2,2],[5,3,2],[5,4,1]]
def M(m,n,A):
    Mij=A
    for i in range(len(A)):
        Mij[i][n:n+1]=[]
    Mij[m:m+1]=[]
    return Mij

def det(A):
    if len(A)==0:
        return 1
    else:
        d=0
        for j in range(len(A)):
            d=d+(-1)**j*A[0][j]*det(M(0,j,A))
        return d

But unfortunately it didn’t work, telling me that there is an index error (A[0][j] doesn’t exist), then I found out that the value of A have been changed to [].
I found out that when I use the function M with a matrix in a loop like that:

d=0
        for i in range(3):
            d=M(0,0,B)
            i+=1
        return B, d

the value of the matrix I use (here is B) changes to be equal to d, with no reason I know.
I hope I explained well, sorry for my bad English.


#5

Ok, but what the function does with B mustn’t be stored in B, it must be stored in just d?


#6

here:

d=M(0,0,B)

you call M, if we look at the function:

def M(m,n,A):

we see that argument B at function call gets passed to parameter A, then in M function you change value of A. this:

Mij=A

will not make a copy of a list, it will just create a variable which points to the same list in memory


#7

But here:

Mij=A

I changed the value of Mij to the parameter A (which its value is B) not the value of the parameter? and what makes the value of B change too?
Maybe that I didn’t understand you well…
In your opinion how can i solve it?


#8

nope, all these variables point to the same list in memory, like i said before.

If you live an a house, and give 3 of your friends (A, B and Mij) your address, and then change something about your house (replacing the front door) all 3 friends would notice. Same is happening here, you have one list (your house) a change made to it, would be noticed by all 3 friends, given it involves the same address (same list in memory)

replacing the door is apology for updating a value in list.


#9

If you have a list

l = []

and you do lots of assignments

a = l
b = a
c = b
d = c

… and so on, they’ll all refer to the same value
If you append to that value:

d.append(5)

then, because they all refer to the same value, you’ll see it when you print it

print l  # [5]

#10

I’m really grateful to you both for your help, thanks a lot!
I finally solve it, by making a new matrix (building a new house :nerd_face: ):

A=[[1,2,3],[4,5,6]]
B=[[0 for j in range(len(A[0]))] for i in range(len(A))]
def mc(Ao,Ac):
    for i in range(len(Ac)):
        for j in range(len(Ac[0])):
            Ac[i][j]=Ao[i][j]
>>> A
[[1, 2, 3], [4, 5, 6]]
>>> B
[[0, 0, 0], [0, 0, 0]]
>>> mc(A,B)
>>> B
[[1, 2, 3], [4, 5, 6]]
>>> A[0][0]=100
>>> A
[[100, 2, 3], [4, 5, 6]]
>>> B
[[1, 2, 3], [4, 5, 6]]

#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.