Message Boards Message Boards

0
|
2770 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

Simple patern match?

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...?

POSTED BY: l van Veen
2 Replies

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 BY: David Reiss

By "Cases only" you mean without using Partition? If so, then probably not without getting very creative.

POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract