考虑一下:
Tuples[Range[1, 3], 2]
我想根据以下列表删除一些子列表:
sublistToTemove = {1,2,3,6,8}
期望的输出:
{2,1},{2,2},{3,1}
对应列表的第4,第5和第7个元素。
我试过Drop,Case,Select没有成功,一定是遗漏了什么。
答案 0 :(得分:8)
鉴于你的清单:
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}}
和
In[5]:= sublistToTemove = {1, 2, 3, 6, 8}
Out[5]= {1, 2, 3, 6, 8}
以下是两种方式:
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}}
答案 1 :(得分:7)
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}}