Python - one variable is equal to another variable if it should not

Here is my sample code. It is designed for an iterative procedure for gauss seidel (matrix solver). In fact, when the error is small enough, it breaks out of the while loop.

i=1
while (i>0):
    x_past = x_present

    j=0
    while(j<3):
        value=0
        k=0
        while(k<3):
            if(k!=j):
                if(i==1):
                    if(k>j):
                        value=value+0
                    else:
                        value=value+x_present[k]*eqn[j][k]    
                else:
                    value=value+x_present[k]*eqn[j][k]
            else:
                value=value+eqn[j][k]
            k=k+1
        x_present[j:j+1]=[value]
        j=j+1
    print "X_PAST"
    print x_past
    print "X_PRESENT"
    print x_present    
    if(error(x_past, x_present)<10**-2):
        break;
    i=i+1

I shortened the code to make it more manageable. if you do not understand what makes it is not very important to solve this problem.

Here is the problem. Everytime

x_present[j:j+1]=[value]

executed, x_past becomes equal to x_present. I don’t know why this is so, since the only place I set x_past to x_present is at the top of the loop. If i take

x_past=x_present

x_past never becomes equal to x_present. This makes me think that this is some combination of the two statements that cause the problem.

, x_past = x_ = 0 , . , , , 8 , , .

4 , . python, . !

+5
6

, , . .

, . :

>>> x_present = [4,5,6]
>>>
>>> x_past = x_present
>>>
>>> x_past
[4, 5, 6]
>>>
>>> x_present.append(7)
>>>
>>> x_past
[4, 5, 6, 7]
>>>

, , listcopy = mylist [:].

>>> x_past = x_present[:]
>>> x_past
[4, 5, 6, 7]
>>>
>>> x_present.append(8)
>>>
>>> x_past
[4, 5, 6, 7]
+30

x_past x_present? Python, .NET/Java, ( - ), ( ) , , . , , . , , - ""?

, Python, ...

+4

, : x_past = x_present x_past = x_present[:]. copy Python.

>>> import copy
>>> a = range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = a
>>> a += 10, 11
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> c = copy.copy(a) # shallow copy
>>> c
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> del a[3:]
>>> a
[0, 1, 2]
>>> b
[0, 1, 2]
>>> c
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

, .

- :

import copy
# assert(len(x_present) >= len(eqn))

first = True
while True:
    x_past = copy.copy(x_present) # copy

    for j, eqj in enumerate(eqn):
        x_present[j] = sum(x_present[k] * eqj[k] 
                           for k in range(j if first else len(eqj)) 
                           if k != j)
        x_present[j] += eqj[j] 

    print "X_PAST\n%s\nX_PRESENT\n%s" % (x_past, x_present)
    if allequal(x_past, x_present, tolerance=10**-2):
        break
    first = False

allequal() ( . ( ):

def allequal(x, y, tolerance):
    return (len(x) == len(y) and 
            all(-tolerance < (xx - yy) < tolerance
                for xx, yy in zip(x, y)))
+3

Python . , x_past = x_present .

+1

, x_present . , , x_last = x_present x_last , .. . ?

0

x_past = x_present x_past = [x for x in x_present] , .

- python, , :

greaterthan100 = [x for x in number if x > 100]

notinblacklist = [x for x in mylist if x not in blacklist]

firstchildofbigfamily = [x.child[0] for x in familylist if len(x.child) > 10]

0
source

All Articles