Skip to main content
added 37 characters in body
Source Link
xyz
  • 655
  • 4
  • 38
  • 117

I would like to take the following data:

insertvaluesinsertValues = {r, x};
insertpositionsinsertPositions = {{1}, {5}};
origlistoriginalList = {a, b, c, d, e, f, g};

and generate output that looks like this:

{r,a,b,c,d,x,e,f,g}

Mathematica'sMathematica's Insert command seems limited in that it does not allow you to insert multiple elements simultaneously into different positions in a list.

I wrote an ugly WhileWhile[] loop to accomplish this, and it works, but seems inappropriate for Mathematica's functional approach to coding. Is there a "simpler" way to accomplish this goal with a functional approach? Thank you.

insertvaluesinsertValues = {r, x};
insertpositionsinsertPositions = {{1}, {5}};
origlistoriginalList = {a, b, c, d, e, f, g};

loopcounterloopCounter = 0; poscounter
posCounter = 0;  
final = origlist;originalList;
While[loopcounterWhile[loopCounter < Length[insertvalues]Length[insertValues],
  final = 
   Insert[final, 
 insertValues[[loopCounter + 1]],  
 insertvalues[[loopcounter + 
        1]], (insertpositions[[loopcounterinsertpositions[[loopCounter + 1]] + poscounterposCounter)]; 
  loopcounter++;loopCounter++; poscounter++];
  posCounter++
];

I would like to take the following data:

insertvalues = {r, x};
insertpositions = {{1}, {5}};
origlist = {a, b, c, d, e, f, g};

and generate output that looks like this:

{r,a,b,c,d,x,e,f,g}

Mathematica's Insert command seems limited in that it does not allow you to insert multiple elements simultaneously into different positions in a list.

I wrote an ugly While loop to accomplish this, and it works, but seems inappropriate for Mathematica's functional approach to coding. Is there a "simpler" way to accomplish this goal with a functional approach? Thank you.

insertvalues = {r, x};
insertpositions = {{1}, {5}};
origlist = {a, b, c, d, e, f, g};

loopcounter = 0; poscounter = 0; final = origlist;
While[loopcounter < Length[insertvalues],
  final = 
   Insert[final, 
     insertvalues[[loopcounter + 
        1]], (insertpositions[[loopcounter + 1]] + poscounter)]; 
  loopcounter++; poscounter++];

I would like to take the following data:

insertValues = {r, x};
insertPositions = {{1}, {5}};
originalList = {a, b, c, d, e, f, g};

and generate output that looks like this:

{r,a,b,c,d,x,e,f,g}

Mathematica's Insert command seems limited in that it does not allow you to insert multiple elements simultaneously into different positions in a list.

I wrote an ugly While[] loop to accomplish this, and it works, but seems inappropriate for Mathematica's functional approach to coding. Is there a "simpler" way to accomplish this goal with a functional approach? Thank you.

insertValues = {r, x};
insertPositions = {{1}, {5}};
originalList = {a, b, c, d, e, f, g};

loopCounter = 0; 
posCounter = 0;  
final = originalList;
While[loopCounter < Length[insertValues],
  final = 
   Insert[final, insertValues[[loopCounter + 1]],  
          (insertpositions[[loopCounter + 1]] + posCounter)]; 
  loopCounter++; 
  posCounter++
];
Tweeted twitter.com/#!/StackMma/status/368932169135058944
added 18 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

I would like to take the following data:

insertvalues = {r, x}; insertpositions = {{1}, {5}}; origlist = {a, b, c, d, e, f, g};

insertvalues = {r, x};
insertpositions = {{1}, {5}};
origlist = {a, b, c, d, e, f, g};

and generate output that looks like this:

{r,a,b,c,d,x,e,f,g}

{r,a,b,c,d,x,e,f,g}

Mathematica's InsertInsert command seems limited in that it does not allow you to insert multiple elements simultaneously into different positions in a list.

I wrote an ugly While loop to accomplish this, and it works, but seems inappropriate for Mathematica's functional approach to coding. Is there a "simpler" way to accomplish this goal with a functional approach? Thank you.

insertvalues = {r, x};
insertpositions = {{1}, {5}};
origlist = {a, b, c, d, e, f, g};

loopcounter = 0; poscounter = 0; final = origlist;
While[loopcounter < Length[insertvalues],
  final = 
   Insert[final, 
    insertvalues[[loopcounter + 
       1]], (insertpositions[[loopcounter + 1]] + poscounter)]; 
  loopcounter++; poscounter++];

I would like to take the following data:

insertvalues = {r, x}; insertpositions = {{1}, {5}}; origlist = {a, b, c, d, e, f, g};

and generate output that looks like this:

{r,a,b,c,d,x,e,f,g}

Mathematica's Insert command seems limited in that it does not allow you to insert multiple elements simultaneously into different positions in a list.

I wrote an ugly While loop to accomplish this, and it works, but seems inappropriate for Mathematica's functional approach to coding. Is there a "simpler" way to accomplish this goal with a functional approach? Thank you.

insertvalues = {r, x};
insertpositions = {{1}, {5}};
origlist = {a, b, c, d, e, f, g};

loopcounter = 0; poscounter = 0; final = origlist;
While[loopcounter < Length[insertvalues],
  final = 
   Insert[final, 
    insertvalues[[loopcounter + 
       1]], (insertpositions[[loopcounter + 1]] + poscounter)]; 
  loopcounter++; poscounter++];

I would like to take the following data:

insertvalues = {r, x};
insertpositions = {{1}, {5}};
origlist = {a, b, c, d, e, f, g};

and generate output that looks like this:

{r,a,b,c,d,x,e,f,g}

Mathematica's Insert command seems limited in that it does not allow you to insert multiple elements simultaneously into different positions in a list.

I wrote an ugly While loop to accomplish this, and it works, but seems inappropriate for Mathematica's functional approach to coding. Is there a "simpler" way to accomplish this goal with a functional approach? Thank you.

insertvalues = {r, x};
insertpositions = {{1}, {5}};
origlist = {a, b, c, d, e, f, g};

loopcounter = 0; poscounter = 0; final = origlist;
While[loopcounter < Length[insertvalues],
  final = 
   Insert[final, 
    insertvalues[[loopcounter + 
       1]], (insertpositions[[loopcounter + 1]] + poscounter)]; 
  loopcounter++; poscounter++];
Source Link
Todd Allen
  • 2.1k
  • 5
  • 29
  • 32

Looking for a way to insert multiple elements into multiple positions simultaneously in a list

I would like to take the following data:

insertvalues = {r, x}; insertpositions = {{1}, {5}}; origlist = {a, b, c, d, e, f, g};

and generate output that looks like this:

{r,a,b,c,d,x,e,f,g}

Mathematica's Insert command seems limited in that it does not allow you to insert multiple elements simultaneously into different positions in a list.

I wrote an ugly While loop to accomplish this, and it works, but seems inappropriate for Mathematica's functional approach to coding. Is there a "simpler" way to accomplish this goal with a functional approach? Thank you.

insertvalues = {r, x};
insertpositions = {{1}, {5}};
origlist = {a, b, c, d, e, f, g};

loopcounter = 0; poscounter = 0; final = origlist;
While[loopcounter < Length[insertvalues],
  final = 
   Insert[final, 
    insertvalues[[loopcounter + 
       1]], (insertpositions[[loopcounter + 1]] + poscounter)]; 
  loopcounter++; poscounter++];