If the elements appear in order:
MapThread[{First[#], Last[#]/Last[#2]} &, {data1, data2}]
If the elements may appear out of order (i.e. the first element in data1
may not correspond to the first element in data2
):
{First[#], Last[#]/Last[#2]} & @@@ GatherBy[Join[data1, data2], First]
If they appear in order and you don't care about creating a variable or modifying an existing one:
data = data1; (* Creates a copy *)
data[[All, 2]] /= data2[[All, 2]];
Or if you want to use rules (this is mostly because we can - it's not necessarily something we should do):
ReplaceList[{data1, data2}, {
{___, {i_, v1_}, ___},
{___, {i_, v2_}, ___}
} :> {i, v1/v2}]
This rule-based one works if the elements appear out order.
Here's another one:
List @@@ Normal[
Association @@ Rule @@@ data1/Association @@ Rule @@@ data2
]
This has an object oriented feel to it and is for those that want to learn about UpValues
:
f /: f[i_, j_]/f[i_, k_] := {i, j/k}
f @@@ data1/f @@@ data2
data1[[All, 2]]
to extract the second parts of each pair, divide, and then useTranspose[]
to reassemble. $\endgroup$