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.
Answer
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}}}
Comments
Post a Comment