RE: There must be a better way!
- To: mathgroup at smc.vnet.net
- Subject: [mg2238] RE: [mg2322] There must be a better way!
- From: "Chandler, Seth" <SChandler at UH.EDU>
- Date: Wed, 25 Oct 1995 02:12:42 -0400
x={A,B,C,D,E,F,G,H,I,J,K,L};
y={2,3,4,3};
whatIwant = Drop[With[{q=FoldList[Drop[#1,#2]&,x,y]},
MapThread[Complement[#1,#2]&,{q,RotateLeft[q]}]],-1]
{{A, B}, {C, D, E}, {I, F, G, H}, {J, K, L}}
----------
> From: mathgroup-adm
To: mathgroup at smc.vnet.net
> To: mathgroup
> Subject: [mg2322] There must be a better way!
> Date: Tuesday, October 24, 1995 2:14AM
>
>
> Hi. I'm trying to write a function that partitions a list into
> segments of length given by another length. eg.
> Given l1 = {A,B,C,D,E,F,G,H,I,J}
> and l2 = {2,3,4,1}
> I want to be able to say
> In[1] := MyPartition[l1, l2]
> and get
> Out[1] = {{A,B}, {C,D,E}, {F,G,H,I}, {J}}
>
> I've racked my brains, but have been unable to think of an elegant
> solution, but instead managed the following Fortran-like function:
>
> MyPartition[list1_, list2_] :=
> Module[{ans,l},
> ans={};
> l = list1;
> Do [
> AppendTo[ans, Take[l, list2[[i]]]];
> l = Drop[l, list2[[i]]],
> {i, Length[list2]}
> ];
> ans
> ]
>
> Someone put me out of my misery! How can I code this in a more efficient
> manner?!
>
> Paul E Howland Tel. +44 (0)1684 895767
> Long Range Ground Radar Sensors Fax. +44 (0)1684 896315
> LSC2 Division Email. PEHOWLAND at DRA.HMG.GB
> Defence Research Agency
> St Andrews Road
> Malvern
> Worcestershire, WR14 3PS, UK
> =========================================================================
>
>