6
$\begingroup$

I would like to compare two different lists to see if the shorter one is a member of the larger e.g. is the list {2,3}, with that sequence of elements, a member of {1,2,3,4,5}? I have tried using MemberQ[] and Cases[] but they do not seem work when comparing two lists. Thanks for pointing me in the right direction.

$\endgroup$
  • 2
    $\begingroup$ SubsetQ[{1,2,3,4,5},{2,3}] yields True. Unless I'm mistaken, you're looking for a subset test, so this should be exactly what you are looking for. $\endgroup$ – DumpsterDoofus Oct 7 '14 at 20:14
  • 1
    $\begingroup$ MatchQ[{1, 2, 3, 4, 5}, {___, 2, 3, ___}] (with 3 _'s each so it will match even if the longer begins with or ends with the shorter) yields True. That is if you are trying to find if your shorter one appears in that order somewhere within the longer one. $\endgroup$ – Bill Oct 7 '14 at 20:30
4
$\begingroup$

I assume you want to find an exact match, instead of just that individual members of the subset are found in the larger list. This can be quite easily be accomplished with the following:

ClearAll[subsetInOrderQ]
subsetInOrderQ[set_List, subset_List] := MatchQ[set, {___, Sequence @@ subset, ___}]

subsetInOrderQ[{1, 2, 3, 4, 5}, {2, 3}]

(* True *)

subsetInOrderQ[{1, 2, 3, 4, 5}, {2, 4}]

(* False *)

Above can also be written in a form without MatchQ:

ClearAll[subsetInOrderQ]
subsetInOrderQ[{___, subset___, ___}, {subset___}] := True
subsetInOrderQ[_, _] := False
$\endgroup$
4
$\begingroup$

Let me stay very close to your question

short = {2, 3};

long = Range @ 5;

par = Partition[long, Length @ short, 1];

MemberQ[par, short]

True

Expanding a little bit

short = Range[2, 4];
long = Range [5];

par = Partition[long, Length @ short, 1];

MemberQ[par, short]

True

IntervalMemberQ[Interval[{1, 5}], Interval[{2, 4}]]

True

Update

short = Range @ 4;
long =  Range @ 5;

IntervalMemberQ[Interval[long[[{1, -1}]]], Interval[short[[{1, -1}]]]]

True

NumberLinePlot[{
  Interval[ long[[{1, -1}]]],
  Interval[short[[{1, -1}]]]},
 PlotTheme -> "Detailed"]

enter image description here

BTW, an interesting question.

$\endgroup$
  • $\begingroup$ +1. Happy to bump you to 10K :) Congrats $\endgroup$ – RunnyKine Oct 7 '14 at 21:09
  • $\begingroup$ Dear @ RunnyKine - A truly gentlemanly bump. I'm beginning to like this club :) $\endgroup$ – eldo Oct 7 '14 at 21:31
  • $\begingroup$ Hi Eldo - I really appreciate your time on this. Thanks so much. Also this is a great place to learn. $\endgroup$ – awyr_agored Oct 8 '14 at 8:14
2
$\begingroup$
Union[{1, 2, 3, 4, 5}, {2, 3}] === {1, 2, 3, 4, 5}
(*True*)
$\endgroup$
  • $\begingroup$ Hi Algohi - Thanks for helping. The problem with the above is if you have the same sub-set appearing again: Union[{1, 2, 3, 4, 5, 6, 5, 3, 2, 3}, {2, 3}] === {1, 2, 3, 4, 5, 6} you get 'false' $\endgroup$ – awyr_agored Oct 8 '14 at 8:12
  • $\begingroup$ If the two lists are sorted then you can use Union[list, {2, 3}] === Union[list] $\endgroup$ – Algohi Oct 9 '14 at 16:54
2
$\begingroup$

Version 10

ClearAll[foo]
foo = Or@@(SubsetQ @@ # &/@{{##},{#2, #}})&

{foo[{1,2,3,4},{3,4}],foo[{2,1},{1,2,3,4}]}
(* True, True *)

Version 9

ClearAll[sQ, foo2]
sQ = And @@ Function[{x}, MatchQ[x, Alternatives @@ #]] /@ #2 &;
foo2 = Or @@ (sQ @@ # & /@ {{##}, {#2, #}}) &

{foo2[{1, 2, 3, 4}, {2, 3}], foo2[{3, 1}, {1, 2, 3, 4}]}
(* {True, True} *)

or

ClearAll[foo3];
foo3 = With[{x = ##}, Or @@ (Intersection@x == Union@# & /@ {x})] &

{foo3[{1, 2, 3, 4}, {2, 3}], foo3[{3, 1}, {1, 2, 3, 4}]}
(* {True, True} *)

or (* generalizing @Algohi's answer to allow arguments in any order *):

ClearAll[foo4];
foo4 = With[{x = ##}, Or @@ (Union@x == Union@# & /@ {x})] &

{foo4[{1, 2, 3, 4}, {2, 3}], foo4[{3, 1}, {1, 2, 3, 4}]}
(*{True,True}*)
$\endgroup$
  • $\begingroup$ Thanks Kguler, everyone has been so helpful. I'll go through the code you provided. Appreciate your time. $\endgroup$ – awyr_agored Oct 8 '14 at 8:19
  • $\begingroup$ @awyr_agored, my pleasure. Welcome to mma.se. $\endgroup$ – kglr Oct 8 '14 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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