Transforming a list to list of lists using a specific ruleSplitting up delimited data in listsSelecting a list from a list of listsPartitioning time series data in sublists by their durationFinding pairs where the intersection of them is empty set from a nested listWay to generate all multisetsGrouping by total length of elements groupedHow to split list into segments based on first element of sublist, and remove ineligible segmentsGathering a List efficientlyRandomly merge elements from paired lists to a new paired listSelecting elements satisfying a condition

Managing heat dissipation in a magic wand

JavaScript: Access 'this' when calling function stored in variable

Download app bundles from App Store to run on iOS Emulator on Mac

Shell builtin `printf` line limit?

Ribbon Cable Cross Talk - Is there a fix after the fact?

Proto-Indo-European (PIE) words with IPA

Why is this python script running in background consuming 100 % CPU?

Coloring lines in a graph the same color if they are the same length

Is there an idiom that means that you are in a very strong negotiation position in a negotiation?

Variable does not Exist: CaseTrigger

A nasty indefinite integral

nginx conf: http2 module not working in Chrome in ubuntu 18.04

Why is 'additive' EQ more difficult to use than 'subtractive'?

Real Analysis: Proof of the equivalent definitions of the derivative.

Does the fact that we can only measure the two-way speed of light undermine the axiom of invariance?

Which are the advantages/disadvantages of includestandalone?

Is it normal to "extract a paper" from a master thesis?

amsmath: How can I use the equation numbering and label manually and anywhere?

Surface of the 3x3x3 cube as a graph

Existence of a model of ZFC in which the natural numbers are really the natural numbers

size of pointers and architecture

why "American-born", not "America-born"?

Does ls -R make any sense with -d?

Salesforce bug enabled "Modify All"



Transforming a list to list of lists using a specific rule


Splitting up delimited data in listsSelecting a list from a list of listsPartitioning time series data in sublists by their durationFinding pairs where the intersection of them is empty set from a nested listWay to generate all multisetsGrouping by total length of elements groupedHow to split list into segments based on first element of sublist, and remove ineligible segmentsGathering a List efficientlyRandomly merge elements from paired lists to a new paired listSelecting elements satisfying a condition













4












$begingroup$


Say, we have a list: l = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 and a corresponding list: v = 2,3,2,3,4. We want to split l into sublists of certain length of consecutive elements ofl and v basically tells us what that length should be. So, in this case, our output would be:



1,2,3,4,5,6,7,8,9,10,11,12,13,14


I have written a function that works:



g[l_List] := Module[split,
split =Table[, i,1,Length @ l];
split[[1]] = Table[i,i,1,First @ l];
Table[split[[i+1]] = Table[j,j,Last @ split[[i]] + 1 ,Total @ l[[1;;i+1]]],
i,1,Length@l - 1];
split
]

g@v



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14




But I'm sure this could be done in a nicer, and more importantly, more efficient way. Any hints?










share|improve this question









$endgroup$







  • 1




    $begingroup$
    It might be worth thinking about what happens when Total@v does not equal Length@l.
    $endgroup$
    – geordie
    May 8 at 12:07






  • 1




    $begingroup$
    In my case that is always the case, but yeah, it's a valid point.
    $endgroup$
    – amator2357
    May 8 at 12:19















4












$begingroup$


Say, we have a list: l = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 and a corresponding list: v = 2,3,2,3,4. We want to split l into sublists of certain length of consecutive elements ofl and v basically tells us what that length should be. So, in this case, our output would be:



1,2,3,4,5,6,7,8,9,10,11,12,13,14


I have written a function that works:



g[l_List] := Module[split,
split =Table[, i,1,Length @ l];
split[[1]] = Table[i,i,1,First @ l];
Table[split[[i+1]] = Table[j,j,Last @ split[[i]] + 1 ,Total @ l[[1;;i+1]]],
i,1,Length@l - 1];
split
]

g@v



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14




But I'm sure this could be done in a nicer, and more importantly, more efficient way. Any hints?










share|improve this question









$endgroup$







  • 1




    $begingroup$
    It might be worth thinking about what happens when Total@v does not equal Length@l.
    $endgroup$
    – geordie
    May 8 at 12:07






  • 1




    $begingroup$
    In my case that is always the case, but yeah, it's a valid point.
    $endgroup$
    – amator2357
    May 8 at 12:19













4












4








4


1



$begingroup$


Say, we have a list: l = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 and a corresponding list: v = 2,3,2,3,4. We want to split l into sublists of certain length of consecutive elements ofl and v basically tells us what that length should be. So, in this case, our output would be:



1,2,3,4,5,6,7,8,9,10,11,12,13,14


I have written a function that works:



g[l_List] := Module[split,
split =Table[, i,1,Length @ l];
split[[1]] = Table[i,i,1,First @ l];
Table[split[[i+1]] = Table[j,j,Last @ split[[i]] + 1 ,Total @ l[[1;;i+1]]],
i,1,Length@l - 1];
split
]

g@v



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14




But I'm sure this could be done in a nicer, and more importantly, more efficient way. Any hints?










share|improve this question









$endgroup$




Say, we have a list: l = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 and a corresponding list: v = 2,3,2,3,4. We want to split l into sublists of certain length of consecutive elements ofl and v basically tells us what that length should be. So, in this case, our output would be:



1,2,3,4,5,6,7,8,9,10,11,12,13,14


I have written a function that works:



g[l_List] := Module[split,
split =Table[, i,1,Length @ l];
split[[1]] = Table[i,i,1,First @ l];
Table[split[[i+1]] = Table[j,j,Last @ split[[i]] + 1 ,Total @ l[[1;;i+1]]],
i,1,Length@l - 1];
split
]

g@v



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14




But I'm sure this could be done in a nicer, and more importantly, more efficient way. Any hints?







list-manipulation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 8 at 10:30









amator2357amator2357

66810




66810







  • 1




    $begingroup$
    It might be worth thinking about what happens when Total@v does not equal Length@l.
    $endgroup$
    – geordie
    May 8 at 12:07






  • 1




    $begingroup$
    In my case that is always the case, but yeah, it's a valid point.
    $endgroup$
    – amator2357
    May 8 at 12:19












  • 1




    $begingroup$
    It might be worth thinking about what happens when Total@v does not equal Length@l.
    $endgroup$
    – geordie
    May 8 at 12:07






  • 1




    $begingroup$
    In my case that is always the case, but yeah, it's a valid point.
    $endgroup$
    – amator2357
    May 8 at 12:19







1




1




$begingroup$
It might be worth thinking about what happens when Total@v does not equal Length@l.
$endgroup$
– geordie
May 8 at 12:07




$begingroup$
It might be worth thinking about what happens when Total@v does not equal Length@l.
$endgroup$
– geordie
May 8 at 12:07




1




1




$begingroup$
In my case that is always the case, but yeah, it's a valid point.
$endgroup$
– amator2357
May 8 at 12:19




$begingroup$
In my case that is always the case, but yeah, it's a valid point.
$endgroup$
– amator2357
May 8 at 12:19










1 Answer
1






active

oldest

votes


















8












$begingroup$

you can use this built-in



 TakeList[l,v] 



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14




the above solution works with version 11.3



If you have an older version try



FoldPairList[TakeDrop,l,v] 



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14







share|improve this answer











$endgroup$












  • $begingroup$
    Brilliant, thank you!
    $endgroup$
    – amator2357
    May 8 at 10:34











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f197933%2ftransforming-a-list-to-list-of-lists-using-a-specific-rule%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









8












$begingroup$

you can use this built-in



 TakeList[l,v] 



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14




the above solution works with version 11.3



If you have an older version try



FoldPairList[TakeDrop,l,v] 



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14







share|improve this answer











$endgroup$












  • $begingroup$
    Brilliant, thank you!
    $endgroup$
    – amator2357
    May 8 at 10:34















8












$begingroup$

you can use this built-in



 TakeList[l,v] 



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14




the above solution works with version 11.3



If you have an older version try



FoldPairList[TakeDrop,l,v] 



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14







share|improve this answer











$endgroup$












  • $begingroup$
    Brilliant, thank you!
    $endgroup$
    – amator2357
    May 8 at 10:34













8












8








8





$begingroup$

you can use this built-in



 TakeList[l,v] 



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14




the above solution works with version 11.3



If you have an older version try



FoldPairList[TakeDrop,l,v] 



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14







share|improve this answer











$endgroup$



you can use this built-in



 TakeList[l,v] 



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14




the above solution works with version 11.3



If you have an older version try



FoldPairList[TakeDrop,l,v] 



1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14








share|improve this answer














share|improve this answer



share|improve this answer








edited May 8 at 10:34

























answered May 8 at 10:32









J42161217J42161217

5,280425




5,280425











  • $begingroup$
    Brilliant, thank you!
    $endgroup$
    – amator2357
    May 8 at 10:34
















  • $begingroup$
    Brilliant, thank you!
    $endgroup$
    – amator2357
    May 8 at 10:34















$begingroup$
Brilliant, thank you!
$endgroup$
– amator2357
May 8 at 10:34




$begingroup$
Brilliant, thank you!
$endgroup$
– amator2357
May 8 at 10:34

















draft saved

draft discarded
















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f197933%2ftransforming-a-list-to-list-of-lists-using-a-specific-rule%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?