2
$\begingroup$

I have a set of six lists shown below. I'm looking for the following: For each list create a subset of that list such that no element in the subset is found in any lower list.

For example, for each element shown in list6, (1) create a subset of that list whose elements are not found in any lower list (list5, list4, list3, list2, list1) and (2) provide a table that shows the lower lists where these excluded elements are found.

Another example: Consider list 3, (1) the subset would be {2/9,2/7,4/9,4/7,6/7,8/9} (2) the excluded elements in list 3 are {0,2/3} where 0 is in list2 and list1 and 2/3 is in list2 and list1.

Here is the code I wrote that attempts to do part (1). I'd like this to be more efficient and I need advice of part (2). Most appreciated for assistance!!

list1={{0},{2/3}}; list2={{0},{2/5},{2/3},{4/5}}; list3={{0},{2/9},{2/7},{4/9},{4/7},{2/3},{6/7},{8/9}}; list4={{0},{2/17},{2/15},{4/17},{4/15},{6/17},{2/5},{8/17},{8/15},{10/17},{2/3},{12/17},{4/5},{14/17},{14/15},{16/17}}; list5={{0},{2/33},{2/31},{4/33},{4/31},{2/11},{6/31},{8/33},{8/31},{10/33},{10/31},{4/11},{12/31},{14/33},{14/31},{16/33},{16/31},{6/11},{18/31},{20/33},{20/31},{2/3},{22/31},{8/11},{24/31},{26/33},{26/31},{28/33},{28/31},{10/11},{30/31},{32/33}}; list6={{0},{2/65},{2/63},{4/65},{4/63},{6/65},{2/21},{8/65},{8/63},{2/13},{10/63},{12/65},{4/21},{14/65},{2/9},{16/65},{16/63},{18/65},{2/7},{4/13},{20/63},{22/65},{22/63},{24/65},{8/21},{2/5},{26/63},{28/65},{4/9},{6/13},{10/21},{32/65},{32/63},{34/65},{34/63},{36/65},{4/7},{38/65},{38/63},{8/13},{40/63},{42/65},{2/3},{44/65},{44/63},{46/65},{46/63},{48/65},{16/21},{10/13},{50/63},{4/5},{52/63},{54/65},{6/7},{56/65},{8/9},{58/65},{58/63},{12/13},{20/21},{62/65},{62/63},{64/65}}; comp=Flatten[Complement[Union[list1,list2],Intersection[Union[list1]]]] Length[comp] comp=Flatten[Complement[Union[list1,list2,list3],Intersection[Union[list1,list2]]]] Length[comp] comp=Flatten[Complement[Union[list1,list2,list3,list4],Intersection[Union[list1,list2,list3]]]] Length[comp] comp=Flatten[Complement[Union[list1,list2,list3,list4,list5],Intersection[Union[list1,list2,list3,list4]]]] Length[comp] comp=Flatten[Complement[Union[list1,list2,list3,list4,list5,list6],Intersection[Union[list1,list2,list3,list4,list5]]]] Length[comp] {2/5,4/5} 2 {2/9,2/7,4/9,4/7,6/7,8/9} 6 {2/17,2/15,4/17,4/15,6/17,8/17,8/15,10/17,12/17,14/17,14/15,16/17} 12 {2/33,2/31,4/33,4/31,2/11,6/31,8/33,8/31,10/33,10/31,4/11,12/31,14/33,14/31,16/33,16/31,6/11,18/31,20/33,20/31,22/31,8/11,24/31,26/33,26/31,28/33,28/31,10/11,30/31,32/33} 30 {2/65,2/63,4/65,4/63,6/65,2/21,8/65,8/63,2/13,10/63,12/65,4/21,14/65,16/65,16/63,18/65,4/13,20/63,22/65,22/63,24/65,8/21,26/63,28/65,6/13,10/21,32/65,32/63,34/65,34/63,36/65,38/65,38/63,8/13,40/63,42/65,44/65,44/63,46/65,46/63,48/65,16/21,10/13,50/63,52/63,54/65,56/65,58/65,58/63,12/13,20/21,62/65,62/63,64/65} 54 
$\endgroup$
1
  • $\begingroup$ List Manipulation is a tag. Please improve the title and make it it more specific to the problem. Thanks. $\endgroup$ Commented Feb 19 at 4:23

1 Answer 1

2
$\begingroup$

For part (1), you can simplify it to this:

Complement[Flatten[listN], Flatten[listN-1], ... , Flatten[list1]] 

For example,

Complement[Flatten[list4], Flatten[list3], Flatten[list2], Flatten[list1]] (* {2/17, 2/15, 4/17, 4/15, 6/17, 8/17, 8/15, 10/17, 12/17, 14/17, 14/15, 16/17} *) 

You can make automating the whole thing easier if you put all the lists into one structure, or if you defined each list like list[1], list[2], etc instead of list1, list2,...

allLists = {list1, list2, list3, list4, list5, list6}; FoldPairList[{Complement[Flatten[#2], #1], Union[Flatten[#2], #1]} &, {}, allLists] (* The result is the aggregation of all of your individual comp *) 
$\endgroup$
8
  • $\begingroup$ Thank u!! ... Any suggestions for Part (2)? $\endgroup$ Commented Feb 18 at 2:04
  • $\begingroup$ I don't understand part two. How do you want that to look? I.e. what structure does it have? And can you give an explicit example, say for the first couple of iterations? $\endgroup$ Commented Feb 18 at 2:14
  • $\begingroup$ Consider list 3, (1) the subset would be {2/9,2/7,4/9,4/7,6/7,8/9} (2) the excluded elements in list 3 are {0,2/3} where 0 is in list2 and list1 and 2/3 is in list2 and list1. $\endgroup$ Commented Feb 18 at 2:21
  • $\begingroup$ Okay, but how do you want that to look--what structure, an association? How do you want to handle elements found in multiple lists (if that's possible)? Show me as a data structure what it means to say "excluded elements in list 3 are {0,2/3} where 0 is in list2 and list1 and 2/3 is in list2 and list1". $\endgroup$ Commented Feb 18 at 2:31
  • $\begingroup$ List 2: Excluded Elements 0 (list1), 2/3 (list1) List 3: Excluded Elements 0 (list2,list1), 2/3 (list2,list1). This could be a table too. $\endgroup$ Commented Feb 18 at 2:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.