Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Let there be two lists that have sublists:

a[] = {{1, 2}, {1, 2, 3}, {2, 3, 4, 4}, {3, 4, 4, 5}}

and

b[] = { {1, 2}, {4, 5, 6}}

I want to remove all those sublists in a[] that has elements contained in a sublist of b[]. So after removal I am expecting

a[] = {{2, 3, 4, 4}, {3, 4, 4, 5}}.

I am not able to reach specific elements in a sublist. I can use a For loop for this, but it will slow down my program, so I want suggestions on a better Mathematica way of manipulating lists.

Edit

Sorry I guess my example was less than clear about what I am asking for. I want that any sublist in a[] that has all elements of any sublist from b[] shall be removed.

share|improve this question
    
Are you sure you want to write a[] and b[]? You aren't defining functions or using indexing, so simple identifiers such as a and b would be better in your case. –  m_goldberg Jun 1 '13 at 6:12
    
ya actually they give same result. –  Rorschach Jun 1 '13 at 6:41
1  
Not under all circumstances. Consider Clear[a]; a[] = 42; a = a[]; {a, a[]}. This gives {42, 42[]} as its output. Is that what you would expect or would want? –  m_goldberg Jun 1 '13 at 23:32
    
thanks for correcting me precisely. –  Rorschach Jun 2 '13 at 6:37
add comment

1 Answer

up vote 4 down vote accepted

Mathematica supports the complement of a set:

Complement[{{1, 2}, {2, 3, 4, 4}, {3, 4, 4, 5}}, {{1, 2}, {4, 5, 6}}]
   {{2, 3, 4, 4}, {3, 4, 4, 5}}

For the elaborated version, you can use Complement[] in tandem with DeleteCases[]:

DeleteCases[{{1, 2}, {1, 2, 3}, {2, 3, 4, 4}, {3, 4, 4, 5}}, 
            l_List /; Or @@ (Complement[#, l] === {} & /@ {{1, 2}, {4, 5, 6}})]
   {{2, 3, 4, 4}, {3, 4, 4, 5}}
share|improve this answer
    
I have amended my question..can you please look into it ? –  Rorschach May 31 '13 at 18:18
    
You should've been explicit to begin with... wait. –  J. M. May 31 '13 at 18:20
    
thank you so much....! –  Rorschach May 31 '13 at 18:32
    
Complement[#, l] === {} signifies what..can u someone explain how its working? –  Rorschach Jun 8 '13 at 14:18
1  
In the interest of teaching you how to fish: start by comparing the result of Complement[#, {1, 2}] === {} & /@ {{1, 2}, {4, 5, 6}} and Complement[#, {2, 3, 4, 4}] === {} & /@ {{1, 2}, {4, 5, 6}} –  J. M. Jun 8 '13 at 15:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.