Re: Extracting information from lists

*To*: mathgroup at smc.vnet.net*Subject*: [mg63354] Re: Extracting information from lists*From*: Bill Rowe <browe51 at earthlink.net>*Date*: Sat, 24 Dec 2005 16:03:01 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

On 12/24/05 at 7:18 AM, mathstutoring at ntlworld.com (Tony King) wrote: >Does anyone know of a function that will allow me to extract the >positions of the first elements of runs of similar elements within >a list. For example, suppose that >list={7,9,1,6,8,1,1,6,5,1,1,1,8,7,6,1,1,1,1,7} >I need a function that will return >{3,6,10,16} >corresponding to the runs {1},{1,1},{1,1,1},{1,1,1,1} within list I am not aware of any built-in function that will do this. But it is fairly easy to create a function to do this, i.e., In[13]:= runStart[data_List] := First/@Split[Flatten[ Position[list, Cases[Split@data, {_, __}][[1,1]]]], #2 - #1 < 2 & ] In[14]:=runStart[list] Out[14]={3, 6, 10, 16} The inner most Split groups runs together. Cases is used to select the runs (assumed to be of a single value}. Position finds the first value of the first run found by Cases. The outer most Split groups the positions into sequential blocks. And finally, the first element of each of these groups is the desired data.