I have, in Wolfram Mathematica 8.0, a nested list like
nList = {{a,b},{f,g},{n,o}}
and a normal list like
lList = {a,b,c,k,m,n,o,z}
and i want to check if all the sublists in nList are in lList (in the example a,b and n,o are there but not f,g)
I’ve done it using For[,,,] and using index… can someone enlighten me in using functions like Map/Thread/Select to do it in one pass.
Edit: If nList contains a,b, lList must contain a,b and not a,c,b or b,a or b,c,a
Assuming that you don’t care about element ordering, here is one way:
EDIT
If the order matters, then here is one way:
For large number of sub-lists, this may be faster:
This works as follows: ReplaceList is a very nice but often ignored command which returns a list of all possible expressions which could be obtained with the pattern-matcher trying to apply the rules in all possible ways to an expression. This is in contrast with the way the pattern-matcher usually works, where it stops upon the first successful match. The PatternSequence is a relatively new addition to the Mathematica pattern language, which allows us to give an identity to a given sequence of expression, treating it as a pattern. This allowed us to construct the alternative pattern, so the resulting pattern is saying: the sequence of any sublist in any place in the main list is collected and put back to list braces, forming back the sublist. We get as many newly formed sublists as there are sequences of the original sublists in the larger list. If all sublists are present, then
Unionon the resulting list should be the same asUnionof the original sublist list.Here are the benchmarks (I took a list of integers, and overlapping sublists generated by
Partition):Conceptually, the reason why the second method is faster is that it does its work in the single run through the list (performed internally by
ReplaceList), while the first solution explicitly iterates through the big list for each sub-list.EDIT 2 – Performance
If performance is really an issue, then the following code is yet much faster:
For example, on our benchmarks:
EDIT 3
Per suggestion of @Mr.Wizard, the following performance improvement can be made:
Here, the as soon as we get a negative answer from sub-lists of a given length, sublists of other lengths will not be checked, since we already know that the answer is negative (
False). IfScancompletes withoutReturn, it will returnNull, which will mean thatlListcontains all of the sublists innList.