4
$\begingroup$

This might seem like a basic operation I have already searched for similar answers but found none. I want to subtract the second elements of my list ct from the corresponding element in lt, and letting the first element to remain as they are the same.

lt={{{1, 1}}, {{2, 1}}, {{3, 1}}, {{2, 3}}, {{5, 1}}, {{2, 4}, {3, 2}}, {{7, 1}}, {{2, 7}}};


ct = {{{1, 1}}, {{2, 1}}, {{3, 1}}, {{2, 2}}, {{5, 1}}, {{2, 1}, {3, 1}}, {{7, 1}}, {{2, 3}}};

as you can see the first elements are the same I want them to remain.

desired result

nt={{{1, 0}}, {{2, 0}}, {{3, 0}}, {{2, 1}}, {{5, 0}}, {{2, 3}, {3, 1}}, {{7, 0}}, {{2, 4}}};

also note that my resulting list nt is my list lt with the last previous first element. like element 4 in my nt is just my element 2 in lt. and where the second element is 0 that means there is no previous first element of the same value, that is a next way to look at it.

$\endgroup$
2
  • $\begingroup$ Can you clarify what you mean in the second part? This is rather opaque to me (on a Sunday evening ;). $\endgroup$
    – Ymareth
    Apr 27, 2014 at 18:06
  • $\begingroup$ oh sorry I just mean that to get my new list nt, I can just use my list lt by looking at the last previous match first elements value. so in my list lt, my first element is {{1,1}} since there is no previouse first value of one, the first element of nt will be {{1,0}} same with element 2 and and 3. but when you reach lt of element four which is {{2,2}}, the last previous element with 2 being the first value is element 2, so nt element four is element 2 of lt which is {{2,1}}. $\endgroup$
    – Crisp
    Apr 27, 2014 at 18:43

4 Answers 4

5
$\begingroup$

You can do:

lt[[;; , ;; , 2]] -= ct[[;; , ;; , 2]];
lt
{{{1, 0}}, {{2, 0}}, {{3, 0}}, {{2, 1}}, {{5, 0}}, {{2, 3}, {3, 1}}, {{7, 0}}, {{2, 4}}}
$\endgroup$
1
  • $\begingroup$ thank you very much! you are correct kuba! $\endgroup$
    – Crisp
    Apr 27, 2014 at 18:51
2
$\begingroup$

Probably not the most elegant way, but works for uneven depths:

rs = lt;
MapIndexed[ If [Element[#1, Integers] && Last@#2 == 2, 
            rs[[Sequence @@ #2]] -= ct[[Sequence @@ #2]]] &, lt, Infinity];
rs
(*
{{{1, 0}}, {{2, 0}}, {{3, 0}}, {{2, 1}}, {{5, 0}}, {{2, 3}, {3, 1}}, {{7, 0}}, {{2, 4}}}
*)
$\endgroup$
1
  • $\begingroup$ @Crisp You can upvote (gray triangle over the score) answers you find useful or are correct. $\endgroup$
    – Kuba
    Apr 27, 2014 at 18:57
2
$\begingroup$

By substitution of zero...

lt-(ct /. {_Integer,x_Integer}:>{0,x})
$\endgroup$
2
  • $\begingroup$ nt===% gives False. There is a sneaky {{2, 4}, {3, 2}} while {{2, 4}}, {{3, 2}} would be expected. $\endgroup$
    – Öskå
    Apr 27, 2014 at 17:59
  • $\begingroup$ Corrected now I think. Glad someone is checking! $\endgroup$
    – Ymareth
    Apr 27, 2014 at 18:18
2
$\begingroup$
Map[MapThread[{First[#1], Last[#1] - Last[#2]} &, #] &, Transpose[{lt, ct}]]

{{{1, 0}}, {{2, 0}}, {{3, 0}}, {{2, 1}}, {{5, 0}}, {{2, 3}, {3, 1}}, {{7, 0}}, {{2, 4}}}

$\endgroup$
0

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