Ask Your Question

Modify a set and removing elements

asked 2021-02-22 18:06:08 +0100

Alain Ngalani gravatar image

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)


For a in N:

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


 For a in N:

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]]

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2021-02-22 18:20:43 +0100

updated 2021-02-22 23:07:54 +0100

Here is an easy way to accomplish what you want:

sage: A = [[1], [2]]                                                                        
sage: B = ['a', 'b']                                                                        
sage: [[x + [y] for x in A] for y in B]                                                   
[[[1, 'a'], [2, 'a']], [[1, 'b'], [2, 'b']]]

Or if you like:

C = []
for x in A:
    for y in B:

I think that the problem with your original code is that you are looping over a list while also modifying that list, which is why working with a copy helps. Note also that code like a.append(1) will change the original list a, and that can be confusing.

for x in A:

modifies each list in A, while

for x in A:
    x + [2]

leaves A unchanged.

Edit: this looks like a job for itertools: Create multiple lists A = [1, 2], B = ['a', 'b'], C = ['o', 'p'] and do

from itertools import product
product(A, B, C)

or to get something more explicit:

list(product(A, B, C))

(The product function gives the Cartesian product of the inputs.) If you don't want multiple lists, create a single list C = [[1, 2], ['a', 'b'], ['o', 'p']] and then do product(*C). (Or if you have your two lists A and B as in the comments, you can replace A with A = [x[0] for x in A] and do C = A + B. It's just list manipulation at this point.)

edit flag offensive delete link more


The fact is that I want to change the original set, this because I need to add elements from B, then from C and so on and end up with a set that is just the set of all such lists. The rest is exactly what I needed.

Alain Ngalani gravatar imageAlain Ngalani ( 2021-02-22 20:59:28 +0100 )edit

So what about the last question? How can I set A equal to the resulting "fused" set so that something like this (for A=[[1],[2]], B=[[a,b],[o,p]])

for x in A:
     for n in range(2):
          for y in B[n]:

ends up gving me A=[[1,a,o],[1,a,p],[1,b,o],[1,b,p],[2,a,o],[2,a,p],[2,b,o],[2,b,p]]?

Alain Ngalani gravatar imageAlain Ngalani ( 2021-02-22 21:12:02 +0100 )edit

I came up with this but for some reason it takes ages to run

def Fus(N,M):
   for n in range(l+1):
   for n in range(l):
       for x in E[n]:
           for y in M[n]:
Alain Ngalani gravatar imageAlain Ngalani ( 2021-02-22 22:22:37 +0100 )edit

See my edit. Looks like you want the Cartesian product of [1, 2], ['a', 'b'], and ['o', 'p'].

John Palmieri gravatar imageJohn Palmieri ( 2021-02-22 23:06:19 +0100 )edit

I fee like it's not ideal in my case, the problem is that at the end I want my set, starting from A=[[a_1,...a_n],[b_1,...b_l],[c_1, ....c_m]]

to be made of elements like

[a_i, f(a_i), f^2(a_i),....f^k(a_i), b_j,....g^h(b_j),c_y,.....h^r(c_y)]

so the "simple" cartesian product (that it's indeed a nice solution to the problem you though I had) may not be enough, also beacuse I don't know how many sets are in my set (I mean it changes based on the input)

Alain Ngalani gravatar imageAlain Ngalani ( 2021-02-23 16:19:23 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-02-22 18:06:08 +0100

Seen: 40 times

Last updated: Feb 22