4
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – m_goldberg Jun 1 '13 at 6:12
  • $\begingroup$ ya actually they give same result. $\endgroup$ – Rorschach Jun 1 '13 at 6:41
  • 1
    $\begingroup$ 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? $\endgroup$ – m_goldberg Jun 1 '13 at 23:32
  • $\begingroup$ thanks for correcting me precisely. $\endgroup$ – Rorschach Jun 2 '13 at 6:37
4
$\begingroup$

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}}
$\endgroup$
  • $\begingroup$ I have amended my question..can you please look into it ? $\endgroup$ – Rorschach May 31 '13 at 18:18
  • $\begingroup$ You should've been explicit to begin with... wait. $\endgroup$ – J. M. is away May 31 '13 at 18:20
  • $\begingroup$ Complement[#, l] === {} signifies what..can u someone explain how its working? $\endgroup$ – Rorschach Jun 8 '13 at 14:18
  • 1
    $\begingroup$ 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}} $\endgroup$ – J. M. is away Jun 8 '13 at 15:07
1
$\begingroup$

In versions 10.2+, you can use ContainsAll combined with Pick, Select, Cases and DeleteCases:

Pick[a[], Nor @@ Through[(ContainsAll /@ b[])@#] & /@ a[]]
Select[a[], Nor @@ Through[(ContainsAll /@ b[])@#] &]
Cases[a[], _?(Nor @@ Through[(ContainsAll /@ b[])@#] &)]
DeleteCases[a[], _?(Or @@ Through[(ContainsAll /@ b[])@#] &)]

all give

{{2, 3, 4, 4}, {3, 4, 4, 5}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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