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.
listSplit
function in my third post here :-) $\endgroup$dynP
is about 40-50 % faster on my test:test = Flatten[Range /@ Range[5000]];
, and thendynP[test, Range[5000]]
, and similarly for the internal function. $\endgroup$test
to packed array withDeveloper`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 (becausePart
does not unpack). This allows for much faster execution and vastly more efficient storage as well, even though the resulting array is ragged. $\endgroup$