MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: finding the position of a pattern in list


Gang Ma wrote:

>Hi,
>I am working on a program to do the following: My data is a list of 0  
>and 1. For example, {0,0,1,1,1,0,0,1,1,1,0}. I want to find the  
>positions of all the pattern of {0,1}.  In my previous example, the  
>first {0,1} is at 2 and and the second {0,1} appears at 7. I can  
>write a loop to do this, but I have several thousands such lists,   
>the computation will be time consuming using loop.
>
>My question is whether it is possible to use the pattern match to do  
>this quickly.  If not for the list, do I need to convert the list to  
>strin
>
Here is what I tried using Reap and Sow, although it does not use any 
pattern matching

 Reap[Table[If[list1[[r]]-list1[[r+1]]===-1,Sow[r]],{r,1,Length[list1],1}]][[2]]//Flatten

In[1]:=

list1= {0,0,1,1,1,0,0,1,1,1,0}
Reap[Table[If[list1[[r]]-list1[[r+1]]===-1,Sow[r]],{r,1,Length[list1],1}]][[2]]//Flatten


Out[1]=
{0,0,1,1,1,0,0,1,1,1,0}

Out[2]=
{2,7}

Hope this helps

Pratik


  • Prev by Date: Re: finding the position of a pattern in list
  • Next by Date: Re: finding the position of a pattern in list
  • Previous by thread: Re: finding the position of a pattern in list
  • Next by thread: Re: finding the position of a pattern in list