Searching for an unordered triple in a list of triples
This seems to be a case for OrderlessPatternSequence
tT = {{7, 1, 2}, {7, 2, 3}, {7, 3, 8}, {7, 1, 6}};
MemberQ[tT, {OrderlessPatternSequence[1, 2, 7]}]
True
Or define a function to wrap the second argument of MemberQ
:
foo = {OrderlessPatternSequence @@ ##}&;
MemberQ[tT, foo @ {1,2,7}]
True
MemberQ[tT, foo @ #] & /@ {{1, 2, 7}, {3, 8, 7},{1, 2, 3}}
{True,True,False}
If the order will never matter for your lists, you can use an Orderless
head rather than list.
I use slist
(sorted list) :
ClearAll[slist]
SetAttributes[slist, Orderless]
Then Mathematica will automatically sort any input given to slist
:
slistT = slist @@@ T
{slist[1, 2, 7], slist[2, 3, 7], slist[3, 7, 8], slist[1, 6, 7]}
This is the way Mathematica handles functions like Plus
or Times
which don't care about the order of their elements.
In your case, you can do
MemberQ[slistT, slist[7, 1, 2] ] (* True *)
MemberQ[slistT, slist[7, 2, 1] ] (* True *)
Another workaround
Xor @@ Sequence[MemberQ[T, #] & /@ Permutations[{7, 2, 1}]]
(* True *)