MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: There must be a better way!

In article <DGxuMB.G69 at>,  <pehowland at> wrote:
>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}}

In the following, I'm using "list1" and "list2" in place of "l1" and
"l2", which look too much like the integers 11 and 12 to me!
(I couldn't for the life of me figuring out why you were calling
MyPartition[11, 12]!!!)

The key idea in the following solution is that you can use
the syntax Take[list, {x, y}] to take the x-th throught y-th element
from a list in a single operation.  If we could somehow turn {2,3,4,1}
into {{1,2}, {3,5}, {6,9}, {10,10}}, then we could simply map
Take[list1, #]& onto that list.  Here goes:

	list1 = {a,b,c,d,e,f,g,h,i,j};
	list2 = {2,3,4,1};

First compute cumulative partial sums of list2:

	FoldList[Plus, First[list2], Rest[list2]]
	{2, 5, 9, 10}

The result is a list of the second elements of the list we're trying
to construct.  Obviously, after we construct the first elements, there's
a Transpose waiting to happen.  Here are the first elements:

	RotateRight[Mod[%,10]+1, 1]
	{1, 3, 6, 10}

(You would want to use Length[list1] in place of 10 in a
production version of this code.)

	Transpose[{%, %%}]
	{{1, 2}, {3, 5}, {6, 9}, {10, 10}}

There you have it:

    Take[list1, #]& /@ %
    {{a, b}, {c, d, e}, {f, g, h, i}, {j}}

		Dave Wagner
		Principia Consulting
		(303) 786-8371
		dbwagner at

  • Prev by Date: Re: Re: Re: Bug in interpretation of mma Series[] command?
  • Next by Date: Mathematica & ODEs+PDEs
  • Previous by thread: Re: There must be a better way!
  • Next by thread: Re: There must be a better way!