Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How can I partition a list into partitions whose sizes vary? The length of the $k$'th partition is a function $f(k)$.

For example: if $l = \{1, 2, 3, 4, 5, 6\}$ and $f(k) = k$. Then the partitioning $p$ would look like $p = \{\{1\},\{2, 3\},\{4,5,6\}\}$

share|improve this question
2  
This question might be useful –  Heike Jun 27 '12 at 9:12
    
This discussion may also be relevant. –  Leonid Shifrin Jun 27 '12 at 9:39
1  
@Mr.Wizard Yes, I can. But then, have a look at the listSplit function in my third post here :-) –  Leonid Shifrin Jun 27 '12 at 9:56
2  
@Mr.Wizard dynP is about 40-50 % faster on my test: test = Flatten[Range /@ Range[5000]];, and then dynP[test, Range[5000]], and similarly for the internal function. –  Leonid Shifrin Jun 27 '12 at 10:08
1  
@Mr.Wizard But, if we convert test to packed array with Developer`ToPackedArray, then the internal function is a little faster. I would generally mention in your answer that for packed arrays, your function creates a ragged list where however all sublists remain packed (because Part does not unpack). This allows for much faster execution and vastly more efficient storage as well, even though the resulting array is ragged. –  Leonid Shifrin Jun 27 '12 at 10:29
show 2 more comments

5 Answers 5

up vote 19 down vote accepted

Update: see section three for a significant optimization.

Reading your question again today I realize that I did not understand it completely the first time. Since my existing answer is already quite long I am posting an additional answer.

This method is not as fast as dynamicPartition but it finally does what you asked.

partitionBy[L_List, func_] := Reap[partitionBy[L, func, 1, 0]][[2, 1]]

partitionBy[L_List, func_, i_, pos_] :=
  With[{x = pos + func[i]},
    partitionBy[Sow @ L[[pos + 1 ;; x]]; L, func, i + 1, x] /; x <= Length@L
  ]

Examples:

partitionBy[Range@10, # &]
{{1}, {2, 3}, {4, 5, 6}, {7, 8, 9, 10}}
partitionBy[Range@10, 2 &]
{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}
partitionBy[Range@12, Mod[#, 3, 1] &]
{{1}, {2, 3}, {4, 5, 6}, {7}, {8, 9}, {10, 11, 12}}

On long lists you may need to increase $IterationLimit.


While I enjoyed writing the functional code above it seems a procedural approach is faster:

partitionBy2[L_List, func_] :=
 Reap[Block[{i = 1, p = 0, x, n = Length@L},
   While[
     (x = p + func[i++]) <= n,
     Sow @ L[[p + 1 ;; (p = x)]];
   ]
 ]][[2, 1]]

Compiled function

For considerably greater speed with compilable length-functions the following may be used:

partitionBy3[L_List, func_] := 
 Inner[L[[# ;; #2]] &, ##, List] & @@ 
  Compile[{{n, _Integer}}, 
    Module[{i = 1},
     {#[[;; -3]] + 1, #[[2 ;; -2]]} & @
       NestWhileList[# + func[i++] &, 0, # <= n &]
    ]
  ] @ Length @ L

Example:

partitionBy2[Range@1*^7, Mod[#, 17, 1] &] // Timing // First

partitionBy3[Range@1*^7, Mod[#, 17, 1] &] // Timing // First

3.76

1.014

share|improve this answer
    
you may be interested to see the definition of Internal'PartitionRagged here in chat –  Jacob Akkerboom Jan 7 at 14:35
    
@Jacob Thanks, I do find that interesting. –  Mr.Wizard Jan 8 at 1:12
add comment

The core solution

If I understand your question I previously wrote a function for this purpose.
The core of that function is:

dynP[l_, p_] := 
 MapThread[l[[# ;; #2]] &, {{0} ~Join~ Most@# + 1, #} & @ Accumulate @ p]

Version 8 users have Internal`PartitionRagged which has the same syntax for the basic case.

dynP[Range@6, {1, 2, 3}]
{{1}, {2, 3}, {4, 5, 6}}
dynP[Range@8, {3, 1, 2, 1}]
{{1, 2, 3}, {4}, {5, 6}, {7}}

Extended version

Since this answer proved popular I decided to do a full rewrite of dynamicPartition:

  • Shorter code with less duplication
  • Better performance and lower argument testing overhead
  • Partitioning of expressions with heads other than List

dynamicPartition[list, runs] splits list into lengths runs.

dynamicPartition[list, runs, All] appends all remaining elements in a single partition.

dynamicPartition[list, runs, spec1, spec2, ...] passes specifications specn to Partition for the remaining elements.

dPcore[L_, p : {q___, _}] := Inner[L[[# ;; #2]] &, {0, q} + 1, p, Head@L]

dPcore[L_, p_, All] := dPcore[L, p] ~Append~ Drop[L, Last@p]

dPcore[L_, p_, n__] := dPcore[L, p] ~Join~ Partition[L ~Drop~ Last@p, n]

dynamicPartition[L_, p : {__Integer}, x___] :=
  dPcore[L, Accumulate@p, x] /; ! Negative@Min@p && Length@L >= Tr@p

(This code no longer uses dynP shown above.)

Usage Examples:

dynamicPartition[Range@12, {4, 3}, All]
{{1, 2, 3, 4}, {5, 6, 7}, {8, 9, 10, 11, 12}}
dynamicPartition[Range@12, {4, 3}, 2]
{{1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {10, 11}}
dynamicPartition[h[1, 2, 3, 4, 5, 6, 7], {3, 1}, 2, 1, 1, "x"]
h[h[1, 2, 3], h[4], h[5, 6], h[6, 7], h[7, "x"]]

Packed arrays

Please note that one special but practically important case is when the list you want to split is a packed array, or can be converted into one. Here is an illustration. First, we create a large (and apparently unpacked) test list:

(test = Flatten[Range/@Range[5000]])//Developer`PackedArrayQ

(*  False  *)

We now split it:

(res = dynP[test,Range[5000]]);//AbsoluteTiming

(* {0.2939453,Null} *)

We can see that the sublists are, or course, unpacked as well:

Developer`PackedArrayQ/@res//Short

(*  
      {False,False,False,False,False,False,False,False,
      <<4984>>,False,False,False,False,False,False,False,False}
*)

Converting to a packed array admittedly takes some time:

test1 = Developer`ToPackedArray[test]; // AbsoluteTiming

(* {0.1660157, Null} *)

But if you do some manipulations with this list many times, this will pay off. Also, often you end up with a packed list from the start. Anyway, now splitting this list is several times faster:

(res1 = dynP[test1,Range[5000]]);//AbsoluteTiming

(*  {0.0644531,Null}  *)

and all the sublists are now also packed:

Developer`PackedArrayQ/@res1//Short

(*
   {True,True,True,True,True,True,True,True,True,
    <<4982>>,True,True,True,True,True,True,True,True,True}
*)

which has a large impact on the total memory consumption as well:

ByteCount/@{res,res1}

(*    {400320040,50900040}    *)

The technique of converting sub-lists of a ragged lists to packed form was already discussed a few times here on SE, e.g. here. In this particular case, dynP will do that automatically when the initial list is packed, but it is still good to keep in mind, for example to avoid accidental unpacking of sublists during whatever further processing you want to perform on the resulting ragged list.

share|improve this answer
1  
+1 very cool! Perhaps for consistency you could implement dynamicPartition[Range@12, {4, 3}, None] and/or dynamicPartition[Range@12, {4, 3}, 0] as well? –  Ajasja Jun 27 '12 at 9:44
    
@Ajasja those would be reasonable extensions, but I think I'll leave it as it is since I want to focus on the core method dynP. I thought it was better to copy the full function here rather than merely reference it on a separate site. –  Mr.Wizard Jun 27 '12 at 9:51
2  
@Ajasja also, I didn't want to clutter the answer further, but since additional arguments are passed to Partition more complicated specifications are possible, e.g. dynamicPartition[Range@20, {4, 3}, 2, 3, 1, "x"] –  Mr.Wizard Jun 27 '12 at 10:09
4  
I'll just note that Wizard's dynP[] is effectively equivalent to the prize-winning solution in the 1992 Mathematica Programming Competition in Rotterdam, with a few modifications. The actual submission used Inner[] instead of MapThread[], but again, the algorithm is identical. –  J. M. Jun 27 '12 at 16:02
2  
@J.M. I'm out of time for today but it looks like Inner is faster than MapThread. I guess this function is due for a rewrite! –  Mr.Wizard Jun 27 '12 at 20:36
show 6 more comments

This is a bit different than Mr.Wizards excellent solution: it calculates the number of successively longer partitions (thus no irregular partitioning argument can be given) using the summation formula, and then does the same Accumulate & extract inside a MapThread.

myPartition[list_] := Module[
   {num = Ceiling[n /. First@Solve[{ n (1 + n)/2 == Length@list, n > 0}]]},
   MapThread[
    Take[list, {#2, Min[Length@list, #2 + #1 - 1]}] &,
    {Range@num, Most@FoldList[Plus, 1, Range@num]}]
   ];

myPartition@Range@20
{{1}, {2, 3}, {4, 5, 6}, {7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}}
share|improve this answer
add comment

This won't win any prizes for performance, but perhaps if there was a prize for using the second argument of Split in ways that were never intended...

partitionBy[list_, func_] :=
 Module[{f, i = func[1], k = 1},
  _f := i-- > 1 || (i = func[++k]);
  Split[list, f]]

partitionBy[Range@12, Mod[#, 3, 1] &]
{{1}, {2, 3}, {4, 5, 6}, {7}, {8, 9}, {10, 11, 12}}
share|improve this answer
2  
That is an odd construction: _f := ... –  rcollyer May 14 '13 at 13:09
    
@rcollyer It may seem odd only because we are used to think of function definitions as if they are similar to other languages. Once we recall that they are rules, this is no more odd than the usual ones. I use this construct (_f:=...) from time to time too. –  Leonid Shifrin May 14 '13 at 13:24
    
@LeonidShifrin I see how it works, but it is still a bit brain warping, though. Not that's a bad thing, just odd to look at. –  rcollyer May 14 '13 at 13:27
    
@rcollyer Yes, I agree. I first saa this, IIRC, in the book of Roman Maeder long time ago, and I had the same feeling back then. –  Leonid Shifrin May 14 '13 at 13:29
1  
@rcollyer, I don't often use it, but here it seemed a natural way to express that the arguments of f are completely irrelevant to its purpose. –  Simon Woods May 14 '13 at 13:52
show 8 more comments

I have created today a question which was a duplicate of this (thanks to Pinguin Dirk).

My attepmts are not very spophisticated but one may find them useful:

Let f[k] be a list of lengths:

l = Range[10];
p = {2, 3, 5};

1

Take[l, {1, 0} + #] & /@ (Partition[Prepend[Accumulate@p, 0], 2, 1])

{{1, 2}, {3, 4, 5}, {6, 7, 8, 9, 10}}

2

FoldList[{Take[#1[[ 2]], #2], Drop[#1[[ 2]], #2]} &, {1, l}, p
        ][[ ;; , 1]] // Rest

{{1, 2}, {3, 4, 5}, {6, 7, 8, 9, 10}}

share|improve this answer
    
Solution 1 nicely explains "the core solution" of Mr.Wizards older answer. –  Jacob Akkerboom Jul 11 '13 at 10:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.