3
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I feel like this is a duplicate. I'll search for it. But here: Internal`PartitionRagged[listJP, Prepend[Differences@Append[pos, Length@listJP], First@pos]]. $\endgroup$ – march Oct 20 '16 at 15:41
7
$\begingroup$
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

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thus it is more simplified so I understand better. $\endgroup$ – JPeter Oct 21 '16 at 10:35
  • $\begingroup$ 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}] $\endgroup$ – JPeter Oct 21 '16 at 11:00
  • $\begingroup$ 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}] $\endgroup$ – JPeter Oct 21 '16 at 11:05
  • 1
    $\begingroup$ @JPeter Now we have a function without Slot $\endgroup$ – LCarvalho Oct 21 '16 at 11:23
5
$\begingroup$

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} *)

Internal`PartitionRagged[listJP, Differences @ pos]
| improve this answer | |
$\endgroup$
  • $\begingroup$ I carelessly did not see your answer uses the same method. I will delete mine. Apologies. $\endgroup$ – ubpdqn Oct 21 '16 at 9:13
4
$\begingroup$

Mapping on Maps... (but uses slots)

listJP[[Span @@ #]] & /@ ({#[[1]] + 1, #[[2]]} & /@ 
 Partition[Flatten[{0, pos, Length@listJP}], 2, 1])
| improve this answer | |
$\endgroup$
2
$\begingroup$

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}}
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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