RE: variation on Split
- To: mathgroup at smc.vnet.net
- Subject: [mg12684] RE: [mg12636] variation on Split
- From: Ersek_Ted%PAX1A at mr.nawcad.navy.mil
- Date: Mon, 1 Jun 1998 23:16:55 -0400
- Sender: owner-wri-mathgroup at wolfram.com
lucb"@ea.com wrote: | |Split[{a,b,c,d,d,e,f,g,g,h,h,h}] returns |{{a},{b},{c},{d,d},{e},{f},{g,g},{h,h,h}} | |What do you have to do to get: |{{a,b,c},{d,d},{e,f},{g,g},{h,h,h}} |? | Here is the way I would do it. __________________________________ In[1]:= lst=Split[{a,b,c,d,d,e,f,g,g,h,h,h}] Out[1]= {{a},{b},{c},{d,d},{e},{f},{g,g},{h,h,h}} The next line splits the list into adjacent lists that have only one element and other lists that have more than one element. In[2]:= ss=Split[lst,(Length[#1]===Length[#2]===1)&] Out[2]= {{{a},{b},{c}},{{d,d}},{{e},{f}},{{g,g}},{{h,h,h}}} Now I apply the function Join to each sublist above. In[3]:= Apply[Join,ss,1] Out[3]= {{a,b,c},{d,d},{e,f},{g,g},{h,h,h}} ________________________________ The next line is a new function that puts all the above in a module. In[4]:= Split2[lst_List]:=Module[{ss}, ss=Split[ Split[lst], (Length[#1]===Length[#2]===1)&]; Apply[Join, ss, 1] ] In[5]:= Split2[{a,b,c,d,d,e,f,g,g,h,h,h}] Out[5]= {{a,b,c},{d,d},{e,f},{g,g},{h,h,h}} ________________________________________ Members of the group can explain further if you don't understand " (Length[#1]===Length[#2]===1)& ". Ted Ersek