4
$\begingroup$

Say I have a set of lists that look like this:

list = {{{151.335`, 245.102`}, {1, 1}}, {{41.435`, 245.021`}, {2, 2}, {3, 3}},
        {{131.048`, 243.364`}, {3, 3}, {56, 56}, {76, 23}}, {{164.911`, 244.039`}, 
         {4, 4}}, {{98.1685`, 239.618`}, {5, 5}}, {{196.333`, 239.212`}, {6, 6}}, 
        {{184.767`, 234.228`}, {7, 7}}, {{213.044`, 234.24`}, {8, 8}}, 
        {{26.6316`, 221.423`}, {9, 9}}};

appendList = {{{3, 3}, {"Yellow", "Red"}, {"Blue", "Majesty"}},
              {{76, 23}, {"White", "Avalanche"}}}

How can I use the first element in each sublist of appendList as an anchor to append the next set of elements in place in list generating something like the following?:

finalList = {{{151.335`, 245.102`}, {1, 1}}, {{41.435`, 245.021`}, {2, 2}, {3, 3},
              {"Yellow","Red"},{"Blue","Majesty"}}, {{131.048`, 243.364`}, {3, 3},
              {56, 56}, {76, 23},{"White","Avalanche"}}, {{164.911`, 244.039`}, 
              {4, 4}}, {{98.1685`, 239.618`}, {5, 5}}, {{196.333`, 239.212`},{6,6}},
             {{184.767`, 234.228`}, {7, 7}}, {{213.044`, 234.24`}, {8, 8}}, 
             {{26.6316`, 221.423`}, {9, 9}}};

After-the-fact question:

This is a brief followup which doesn't quite deserve it's own question - how can we only look at the last element in each sublist of list to check to see if it matches the "anchor" value at the first position in appendList?

In other words, where {{{2, 2}, {"Yellow", "Red"}, {"Blue", "Majesty"}}, {{56, 56}, {"White", "Avalanche"}}} yields a finalList = list since {2,2} and {56,56} are not at the ends of the sublists in list?

$\endgroup$
6
$\begingroup$

One way to do this is the following:

list /. (RuleDelayed[{x__, #[[1]]}, {x}~Join~#] & /@ appendList)
$\endgroup$
4
$\begingroup$

Another approach is using Sequence

list /. (#[[1]] -> Sequence @@ # & /@ appendList)
$\endgroup$
  • $\begingroup$ Ah, the parentheses seem to make it work! $\endgroup$ – HStoley Sep 13 '13 at 20:01
  • $\begingroup$ Do you know a quick fix to address my "after-the-fact question" added to the question body? It seems simple, but I haven't been able to figure out how to do it with a simple one-liner. $\endgroup$ – HStoley Sep 13 '13 at 20:02
  • $\begingroup$ @HStoley I'm already thinking about it. In this case my solution transforms to list /. ({x___, #[[1]]} :> {x, Sequence @@ #} & /@ appendList) which is equivalent to tom's solution :) $\endgroup$ – ybeltukov Sep 13 '13 at 20:07
  • $\begingroup$ Ah, so Tom's solution automatically does this? $\endgroup$ – HStoley Sep 13 '13 at 20:08
  • $\begingroup$ And thanks for thinking about the update. $\endgroup$ – HStoley Sep 13 '13 at 20:09
2
$\begingroup$

Here is my variation of tom's solution. (It too handles the "after-the-fact" addition.)
I use ## (SlotSequence) and @@@ (Apply at level one) for more concise code.

list /. ({x___, #} :> {x, ##} & @@@ appendList)
$\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.