Re: Partition List
- To: mathgroup at smc.vnet.net
- Subject: [mg8433] Re: [mg8398] Partition List
- From: Tom Wickham-Jones <twj>
- Date: Sat, 30 Aug 1997 00:42:42 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Xah wrote: > > A question on programing. > I want to partition a list, using special values in the list as boundary > indicators. For example, myList={3,7,10,6,1,1,6,1,7,10,3,4,6} and my special > value for boundary is 10, then my desired result would be > result={{3,7},{10,6,1,1,6,1,7},{10,3,4,6}}. > > How to do this using structure manipulating operators, and how to do it with > pattern matching? Thanks. In V3.0 there is a function Split which will do what you want. In[5]:= ?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[6]:= myList={3,7,10,6,1,1,6,1,7,10,3,4,6}; In[7]:= Split[ myList, #2 =!= 10&] Out[7]= {{3, 7}, {10, 6, 1, 1, 6, 1, 7}, {10, 3, 4, 6}} Tom Wickham-Jones WRI