RE: Fast List-Selection
- To: mathgroup at smc.vnet.net
- Subject: [mg19898] RE: [mg19880] Fast List-Selection
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Mon, 20 Sep 1999 20:42:24 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Hans Havermann wrote: --------------------------- I have a list 's' composed of a large number of (small) integers. I wish to search this list for instances of 7 consecutive, identical elements. My approach is: Do[If[Count[t = Take[s, {i, i + 6}], t[[1]]] == 7, Print[i]], {i, 1, Length[s] - 6}] Can anyone think of a *faster* way of doing this? -------------------- Split is made for this sort of thing. See the usage message and example below. I would give code to solve the very problem you describe, but I am having a hard time understanding what the code should do. Anyway, using Split should make you code run much faster. In[1]:= ?Split Split[list] splits list into sublists consisting of runs of identical elements. Split[list, test] treats pairs of adjacent elements as identical whenever applying the function test to them yields True. In[2]:= s=Table[Random[Integer,2],{100}] Out[2]= {2,2,0,0,1,0,1,2,2,0,1,0,0,2,0,0,0,0,2,1,2,0,1,0,0, 0,0,2,2,2,1,1,1,2,1,2,1,0,0,2,2,2,2,0,1,1,1,2,1,0,2, 2,2,1,1,2,2,2,2,1,1,2,1,1,1,1,1,1,2,0,2,1,1,0,0,1,2, 0,1,0,0,0,2,0,0,2,0,2,2,1,1,0,2,2,1,2,1,1,2,2} In[3]:= tt=Split[s] Out[3]= {{2,2},{0,0},{1},{0},{1},{2,2},{0},{1},{0,0},{2}, {0,0,0,0},{2},{1},{2},{0},{1},{0,0,0,0},{2,2,2}, {1,1,1},{2},{1},{2},{1},{0,0},{2,2,2,2},{0},{1,1,1}, {2},{1},{0},{2,2,2},{1,1},{2,2,2,2},{1,1},{2}, {1,1,1,1,1,1},{2},{0},{2},{1,1},{0,0},{1},{2},{0}, {1},{0,0,0},{2},{0,0},{2},{0},{2,2},{1,1},{0},{2,2}, {1},{2},{1,1},{2,2}} -------------------- Regards, Ted Ersek For Mathematica tips, tricks see http://www.dot.net.au/~elisha/ersek/Tricks.html