Group Abstract Group Abstract

Message Boards Message Boards

4
|
6.3K Views
|
3 Replies
|
10 Total Likes
View groups...
Share
Share this post:

ContainsAll vs. SubsetQ

I'm having a problem with ContainsAll and I wonder if anyone can explain it. I have list1, a list of lists, and list2, a simple list. For example:

list1={{1,3,7},{4,5,6},{2,3,7},{3,6,7},{3,5,6},{1,4,7}}

list2={3,7}

Thinking of these as sets, suppose I want to find all the sets in list1 that have list2 as a subset. This works:

Select[list1,ContainsAll[list2]]

{{1,3,7},{2,3,7},{3,6,7}}

Another way to get the answer is:

Select[list1,SubsetQ[#,list2]&]

{{1,3,7},{2,3,7},{3,6,7}}

Now suppose I want all sets in list1 for which list 2 is not a subset. This works:

Select[list1,!SubsetQ[#,list2]&]

{{4,5,6},{3,5,6},{1,4,7}}

But negating ContainsAll returns the empty set:

Select[list1,!ContainsAll[list2]]

{}

Any ideas? Is there something about the Attributes of ContainsAll that I'm missing? The documentation says ContainsAll is identical to SubsetQ. But there seems to be a subtle difference between the two.

POSTED BY: John Shonder
3 Replies
Posted 6 years ago

Vitaliy has already given you a working answer. That being said, if you really, truly insist on sticking to using an operator form, you can still make it work through a judicious use of Composition[] (@*); to wit:

Select[list1, Not @* ContainsAll[list2]]
   {{4, 5, 6}, {3, 5, 6}, {1, 4, 7}}

or even

list1 // Select[Not @* ContainsAll[list2]]

which should yield the same result.

POSTED BY: J. M.

That makes sense, thank you Vitaliy.

POSTED BY: John Shonder

This works:

In[]:= Select[list1,!ContainsAll[#,list2]&]
Out[]= {{4,5,6},{3,5,6},{1,4,7}}

and syntax in your variant does not because your are dealing with an operator form ContainsAll[list2] --- you did not use pure function here as in with SubsetQ. You loose operator form if you wrap the operator into another function

In[]:= !ContainsAll[l]//FullForm
Out[]= Not[ContainsAll[l]]

and you need to go to non-operator function definition and pure function syntax to make it to work.

POSTED BY: Vitaliy Kaurov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard