For example I have two lists of such structure
L1 = {{"Australia",a1}, {"USA",a2}, {"Norway",a3},...}
L2 = {{"Russia",b1}, {"Norway",b2}, {"Japan",b3},...}
I would like to sort countries of the list L2
in the countries' order of L1
to compare it entirely (I mean the values matching for each country). Thus I would like to gain something resembling this as a result where L3
is the sorted L2
:
L1 = {{"Australia",a1}, {"USA",a2}, {"Norway",a3},...}
L3 = {{"Australia",b26},{"USA",b13} {"Norway",b2},...}
Answer
l1 = {{"a", a1}, {"b", a2}, {"c", a3}}
l2 = {{"c", b3}, {"b", b2}, {"a", b1}}
{#, # /. Rule @@@ l2} &[l1[[All, 1]]] // Transpose
(*
{{"a", b1}, {"b", b2}, {"c", b3}}
*)
Comments
Post a Comment