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