# Simple patern match?

Posted 9 years ago
2356 Views
|
2 Replies
|
1 Total Likes
|
 I find it silly to post this but it seems I've got a blind spot for this one..Suppose I have a list test. test = {{1, {2}, 3}, {2, 3, 4, 3}, {3, {3}, 4, 3}, {4, 5}, {6, 7}, {4, 5}} Now I would like to find a set of two lists next to eachother where the last item is a 3. The result would be:{{{1, {2}, 3}, {2, 3, 4, 3}},{{2, 3, 4, 3}, {3, {3}, 4, 3}}}I only found that Cases[Partition[test, 2, 1], {{__, 3}, {__, 3}}] does work but I would guess that with Cases only I should be able to get it to work with a correct pattern...?
2 Replies
Sort By:
Posted 9 years ago
 You can use ReplaceList for something like this... Cases is hard. I tried all sort of ways for that, but as Daniel says, it requires more creativity than I certainly have. In[1]:= test = {{1, {2}, 3}, {2, 3, 4, 3}, {3, {3}, 4, 3}, {4, 5}, {6, 7}, {4, 5}} Out[1]= {{1, {2}, 3}, {2, 3, 4, 3}, {3, {3}, 4, 3}, {4, 5}, {6, 7}, {4, 5}} In[2]:= ReplaceList[test, {___, y : {__, 3}, z : {__, 3}, ___} :> {y, z}] Out[2]= {{{1, {2}, 3}, {2, 3, 4, 3}}, {{2, 3, 4, 3}, {3, {3}, 4, 3}}} 
Posted 9 years ago
 By "Cases only" you mean without using Partition? If so, then probably not without getting very creative.