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.
4 Answers
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
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"]
BTW, an interesting question.
-
-
$\begingroup$ Dear @ RunnyKine - A truly gentlemanly bump. I'm beginning to like this club :) $\endgroup$– eldoOct 7, 2014 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$ Oct 8, 2014 at 8:14
Union[{1, 2, 3, 4, 5}, {2, 3}] === {1, 2, 3, 4, 5}
(*True*)
-
$\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$ Oct 8, 2014 at 8:12
-
$\begingroup$ If the two lists are sorted then you can use Union[list, {2, 3}] === Union[list] $\endgroup$ Oct 9, 2014 at 16:54
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}*)
-
$\begingroup$ Thanks Kguler, everyone has been so helpful. I'll go through the code you provided. Appreciate your time. $\endgroup$ Oct 8, 2014 at 8:19
-
SubsetQ[{1,2,3,4,5},{2,3}]
yieldsTrue
. Unless I'm mistaken, you're looking for a subset test, so this should be exactly what you are looking for. $\endgroup$