Please can anyone help ? I have a list of sets, and I am trying to find the positions in that list of those sets which are subsets of a given set. (Here I am ignoring the distinction between lists and sets, possibly at my peril.)
The following code
subsetQ[A_, B_] := Intersection[A, B] == A;
Position[{{a, b}, {b}, {e, f}, {c, e}, {c}}, _?(subsetQ[#, {b, c, e}] &), 1]
gives the output
{{2}, {4}, {5}}
which is exactly what I want.
Many thanks to these contributors
How to find the position of elements in a list satisfying criteria
for showing me the way. However, my code also generates the error message
Intersection::normal: "Nonatomic expression expected at position 1 in List[Intersection]{b,c,e}."
Is there a way to avoid this message, other than suppressing it using
Off[General::normal].
?
I have studied the documentation here:
http://reference.wolfram.com/language/tutorial/PuttingConstraintsOnPatterns.html
to try to understand whether I should use x_/;
or _?()
but I confess I don't understand it well.
Thank you very much.
_?
is faster but is limited in that it requires something that can be applied as a pure function./;
isn't necessarily always slower and offers more flexibility in that you can follow it with any type of expression that returns a Boolean value. I'm curious why you didn't use the built-inSubsetQ
? Are you on an older version? $\endgroup$ – IPoiler Sep 25 '15 at 13:49