I have a list of pairs of strings: myPairsList
. I wish to Select
all the pairs that do not contain "A"
or "B"
. Unlike functions such as Cases
that use patterns as input, the function Select
uses a criterion ("crit") as input.
A verbose way to execute my desired selection is:
Select[myPairsList, (#[[1]] != "A") &&
(#[[1]] != "B") && (#[[2]] != "A") && (#[[2]] != "B") &]
{{"X", "Y"}, {"Y", "X"}}
Perhaps a more elegant way to execute the selection is:
Select[myPairsList, (! MemberQ[{"A", "B"}, #[[1]]]) &&
(! MemberQ[{"A", "B"}, #[[2]]]) &]
{{"X", "Y"}, {"Y", "X"}}
But, what if I want to express the selection as "the first element is neither "A"
nor "B"
, and the second element is neither "A"
nor "B"
"?
To do that, I tried the following:
Select[myPairsList, (#[[1]] != ("A" || "B")) && (#[[2]] != ("A" || "B")) &]
Select[myPairsList, (#[[1]] != ("A" | "B")) && (#[[2]] != ("A" | "B")) &]
{}
{}
But, as you can see, I don't obtain my desired output, so I'm clearly using the incorrect syntax. So what is the correct way to express "not equal to A or B"?
Select[myPairsList, FreeQ[#, "A" | "B"] &]
$\endgroup$