How to insert a string after every 3rd list and then add 0.5 on every Z on every list after?

Input: {"X1","Y1","Z0"},{"X2","Y2","Z0"},{"X3","Y3","Z0"}...{"X100","Y100","Z0"}

output: {"X1","Y1","Z0"},{"X2","Y2","Z0"},{"X3","Y3","Z0"},{"Randomstring"},{X4,Y4,Z0.5}

  • 1
    $\begingroup$ I strongly encourage you to learn the basics about "list manipulation". It's one of the most important things when working with mathematica. $\endgroup$ – Wizard Dec 7 '15 at 9:38
  • $\begingroup$ I've made a partition, then joined the string and made If ContainsStringsQ change Z->Z+0.5 for every 3rd part. But my list is pretty long and it crashes. So I was wondering if there was an easier way or something. $\endgroup$ – EminemIsLife Dec 7 '15 at 9:43
  • $\begingroup$ This is a very ill-posed question. We expect questions to be formulated more precisely than this on this site. Do you really mean {X4, Y4, Z0.5}? Or do you mean {"X4", "Y4", "Z0.5"}? Further, what do you mean by "add 0.5 on every Z after ..."? Do you mean {"X4", "Y4", "Z0.5"}, .., {"X100", "Y100", "Z0.5"}? $\endgroup$ – m_goldberg Dec 7 '15 at 11:32
  • $\begingroup$ I mean "X4" as in a string. Every 3rd element (the last element in the 3rd) in the list should be added by 0.5 Example: {"X4", "Y4", "Z0.5"} .. {"X6", "Y6", "Z1"} $\endgroup$ – EminemIsLife Dec 7 '15 at 11:37
  • $\begingroup$ Does another random string get inserted after {"X6", "Y6", "Z1"}? Or same random string as after` {"X3","Y3","Z0"}`? Please edit your question to clarify your intent. People will often miss clarifications made in a comment. $\endgroup$ – m_goldberg Dec 7 '15 at 12:53
list =
  {{"X1", "Y1", "Z0"}, {"X2", "Y2", "Z0"}, {"X3", "Y3", "Z0"},
   {"X1", "Y1", "Z0"}, {"X2", "Y2", "Z0"}, {"X3", "Y3", "Z1"},
   {"X1", "Y1", "Z0"}, {"X2", "Y2", "Z0"}, {"X3", "Y3", "Z2"}};

step = 3;

Add 0.5 to last element at every step positions

rep =
 StringTake[#, 1] <>
   ToString[ToExpression@FromCharacterCode@Rest@ToCharacterCode[#] + 0.5] & /@
      list[[All, -1]][[step ;; ;; step]]

{"Z0.5", "Z1.5", "Z2.5"}

Replacement positions

pos = Transpose[{#, Array[-1 &, Length@#]}] &[Range[step, Length@list, step]]

{{3, -1}, {6, -1}, {9, -1}}


sol = ReplacePart[list, Dispatch@Thread[pos -> rep]];


Insert[sol, "RandomString", List /@ Range[Length@pos + 1, Length@list, step]] // MatrixForm

enter image description here


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