-2

This is weird, this is a piece of my my code:

 def vert(vert):

        c=[]

        #print b
        for i in range(3):
                c.append(list(vert[i]))
        e=d=c
        s=[]
        print c
        by={0: (1,),1: (0,2),2: (1,)}
        boolean=False
        for i in range(3):
            for j in range(3):
                if c[i][j]==0:
                    boolean=True
                    print i,j
                    for k in by[j]:
                        d[i][j],d[i][k]=d[i][k],d[i][j]
                        print d
                        s+=d
                        d[i][j],d[i][k]=d[i][k],d[i][j]
                    for l in by[i]:
                        e[i][j],e[l][j]=e[l][j],e[i][j]
                        print e
                        s+=e
                        e[i][j],e[l][j]=e[l][j],e[i][j]
                    break;
            if boolean :
                print s
                break;


vert(vertices[0])

the output is:

[[8, 1, 0], [5, 2, 6], [7, 3, 4]]
0 2
[[8, 0, 1], [5, 2, 6], [7, 3, 4]]                                     #d
[[8, 1, 6], [5, 2, 0], [7, 3, 4]]                                     #e 
[[8, 1, 0], [5, 2, 6], [7, 3, 4], [8, 1, 0], [5, 2, 6], [7, 3, 4]]    #s

This is what I don't think should happen, I add e and d to s and they are added in a different form that I don't want it to be, Can anyone see what is going on? what I want is e and d be in the form they are printed. But there was no way I could see how to add them to s in the right form.

Parisa
  • 83
  • 1
  • 9

1 Answers1

4

If you assign a new name to a Python obejct it is not copied but linked. So after e=d=c they all point at the same list. It you change one, you change all. To prevent that initalize them seperatly.

e = []
d = []
c = []
Klaus D.
  • 13,874
  • 5
  • 41
  • 48