Sublist as a function of positions

I have a list as follows:

listJP=Range[50]


{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50}

And I have another list indicating the positions where I want to create sublists

pos={5,12,24,38}


{5,12,24,38}

The values in the above list serve to show what position the list must quit and continue the next.

{list1,list2,list3,list4,list5}=listJP[[#]]&/@Rest@FoldList[Span[#[[-1]]+1,#2]&,{0},Append[Flatten[pos],Length[listJP]]]


{{1,2,3,4,5},{6,7,8,9,10,11,12},{13,14,15,16,17,18,19,20,21,22,23,24},{25,26,27,28,29,30,31,32,33,34,35,36,37,38},{39,40,41,42,43,44,45,46,47,48,49,50}}

This was a very good solution created by @Michael E2, but the Slot function still confuse me and so I would know if there is an in-built to do that.

I looked for something in Split, Partition, SpliBy, but unfortunately I did not succeed.

• I feel like this is a duplicate. I'll search for it. But here: InternalPartitionRagged[listJP, Prepend[Differences@Append[pos, Length@listJP], First@pos]]. – march Oct 20 '16 at 15:41

FoldPairList[TakeDrop, listJP, Flatten@{0, pos, Length@listJP} // Differences]


{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, {25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38}, {39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}}

UPDATE

These are the steps in the process:

FoldPairList[TakeDrop,listJP,Flatten@{0,pos,Length@listJP}//Differences] FoldPairList[TakeDrop,listJP,Flatten@{0,{5,12,24,38},50}//Differences] FoldPairList[TakeDrop,listJP,{0,5,12,24,38,50}//Differences] FoldPairList[TakeDrop,listJP,{5,7,12,14,12}]

Now is simple: the list will be subdivided into sublists with the lengths above. Takes the First 5, then 7, then the 12 ...

The code above uses the positions to define the lengths that the function needs.

And Length@listJP ensures that will be used the elements of the list mentioned

• Thus it is more simplified so I understand better. – JPeter Oct 21 '16 at 10:35
• I found it very interesting this new command, but it is well complex for me. But at least it has little Slot. I tried to understand this test and couldn't: p = Span; q = Plus; FoldPairList[{p[#1, #2], q[#1, #2]} &, u, {a, b, c, d}] – JPeter Oct 21 '16 at 11:00
• I managed to find in the documentation the reference that possibly you have used: FoldPairList[TakeDrop, {a, b, c, d, e, f}, {1, 3, 2}] – JPeter Oct 21 '16 at 11:05
• @JPeter Now we have a function without Slot – LCarvalho Oct 21 '16 at 11:23

With pos = {5, 12, 24, 38}, this will work:

Split[listJP, FreeQ[pos, #] &]


or (the same reasoning, but reversed)

Split[listJP, Not @ MemberQ[pos, #] &]


Or with PartitionRagged, an undocumented yet useful command:

pos = {0}~Join~pos~Join~{Length @ listJP}


or

pos = Join[{0}, pos, {Length @ listJP}]
(* or just: pos = Flatten @ {0, pos, Length @ listJP} *)

InternalPartitionRagged[listJP, Differences @ pos]

• I carelessly did not see your answer uses the same method. I will delete mine. Apologies. – ubpdqn Oct 21 '16 at 9:13

Mapping on Maps... (but uses slots)

listJP[[Span @@ #]] & /@ ({#[[1]] + 1, #[[2]]} & /@
Partition[Flatten[{0, pos, Length@listJP}], 2, 1])


The function dPcore used in my answer to Partitioning with varying partition size does just this:

dPcore[listJP, pos, All]

{{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24},
{25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38},
{39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}}