Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Modify a set and removing elements

I'm trying to modify a set but I'm having some problems doing so (I hope the code her รจ behaves the same as the one I have at home, at the moment I don't have access to the algorithms creating the specific set)

N=[[(0,0,1)],[(0,1,0)],[(0,1,1)],[(1,0,0)],[(1,0,1)],[(0,1,0)],[(1,1,1)]]

For a in N:
     N.remove(a)
     a.append(1)
     N.append(a)

This creates problems given how it doesn't remove all the elements and doesn't modify all of them

I kinda solved by introducing another set as

V=copy(N)

 For a in N:
     V.remove(a)
     a.append(1)
     V.append(a)

Now V behaves well, so I have two questions (plus a bonus one) :

Why does the first method create problems?

Using the second method I would like (the target algorithm does this for a set of a so I need the result to pass through the same process given that I need to add other elements to every eleemnt/set) at the end to set N as equal to V but neither using N=V, N=copy(V) nor N=[] and then appending all the elemts seems to work.

Is there a better way to do what I want to do? So a better way to take elements of a set and obatain another set whose elements are the starting ones modified by adding elements from another set? E. G. starting from A=[[1], [2]] , B=[a, b] obtain A=[[1,a], [1,b], [2,a],[2,b]]