Removing nested lists in Mathematica

Consider:

Tuples[Range[1, 3], 2]

enter image description here

I would like to keep some subscriptions based on the following list:

sublistToTemove = {1,2,3,6,8}

Output Required:

{2, 1}, {2, 2}, {3, 1}

In accordance with the 4th, 5th and 7th elements of the list.

I tried Drop, Case, Select without success, should miss something.

+3
source share
2 answers

Given your list:

In[2]:= lst = Tuples[Range[1, 3], 2]

Out[2]= {{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3,2}, {3, 3}}

and

In[5]:= sublistToTemove = {1, 2, 3, 6, 8}

Out[5]= {1, 2, 3, 6, 8}

Here are two ways:

In[6]:= Delete[lst, List /@ sublistToTemove]

Out[6]= {{2, 1}, {2, 2}, {3, 1}, {3, 3}}

In[7]:= lst[[Complement[Range[Length[lst]], sublistToTemove]]]

Out[7]= {{2, 1}, {2, 2}, {3, 1}, {3, 3}}
+8
source
In[15]:= sublistToTemove = {1, 2, 3, 6, 8};

In[16]:= Delete[Tuples[Range[1, 3], 2], Transpose[{sublistToTemove}]]

Out[16]= {{2, 1}, {2, 2}, {3, 1}, {3, 3}}
+7
source

All Articles