I have a large list of the following form
{{1, 2} -> -1, {1, 1} -> 1, {1, 2} -> -1, {1, 6} -> 1}
and I want to combine all of the pairs x->y
that share the same x
value by adding up their y
values. For the above example I want to have
{{1, 2} -> -2, {1, 1} -> 1, {1, 6} -> 1}
and I want to do it using patterns and ReplaceRepeated
command. I tried the following code:
{{1, 2} -> -1, {1, 1} -> 1, {1, 2} -> -1, {1, 6} -> 1} //.
HoldPattern[{x_ -> y_, a___, x_ -> z_}] -> {x -> y + z, a}
Can anybody tell me why above code is not working and how can I do it using rules?
//.
is not really intended for combinations like these. TryMerge[]
:Normal[Merge[{{1, 2} -> -1, {1, 1} -> 1, {1, 2} -> -1, {1, 6} -> 1}, Total]]
$\endgroup$