4
$\begingroup$

Consider some list of elements

elements={"a","k",1,"12","cdt","b","m","l","q",132,12.345,999,1000,Exp[x^2],Sin[y]};

We may make the following table

tabb = Table[Take[RandomSample[elements],4],50]

and a row

roww = Take[RandomSample[elements],4]

I have to check if roww matches at least one row of tab, now taking into account that the ordering of the elements of roww is not important. I.e., if tabb includes the row {999,1000,Exp[x^2],Sin[y]}, while roww is {1000,Sin[y],999,Exp[x^2]} then the decision should be True.

How to check if at least one ordering of roww matches with at least one row of tabb?

Edit

Probably it is ContainsOnly?

$\endgroup$

4 Answers 4

3
$\begingroup$
MemberQ[Sort /@ tabb, Sort[roww]]
$\endgroup$
2
$\begingroup$
containsInAnyOrder = Not @ FreeQ[{OrderlessPatternSequence @@ #2}] @ # &;
$\endgroup$
1
$\begingroup$
Or @@ Map[ContainsAll[roww], tabb]

Some examples

tabb = {{"m", Sin[y], "q", "k"}, {"q", "cdt", "m", 132}, {"12", "a", "m", "b"}, {"k", 999, 12.345`, 132}};

roww = {132, "m", "q", "cdt"};

Or @@ Map[ContainsAll[roww], tabb]

True

roww = {132, "q", "q", "cdt"};

Or @@ Map[ContainsAll[roww], tabb]

True

roww = {132, "q", "-1", "cdt"};

Or @@ Map[ContainsAll[roww], tabb]

False

$\endgroup$
1
$\begingroup$

My attempt is as follows:

MatchesRowQ[table_, row_] := MemberQ[table, #] & /@ 
                             Permutations[row, Length[row]] // Or @@ # &

Testing MatchesRowQ:

tabb = {{"m", Sin[y], "q", "k"}, {"q", "cdt", "m", 132},
        {"12", "a", "m", "b"}, {"k", 999, 12.345`, 132}};

roww = {132, "m", "q", "cdt"};

MatchesRowQ[tabb, roww]

(*True*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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