This is very similar to my earlier question One to Many Lists Merge but somehow different. I have two lists, first column in each list represents its key. I want to merge these two lists. The only problem is that these two lists have some common keys but not all keys are the same and that they are of different lengths. For example,
list1 = {{1, a, aa}, {2, b, bb}, {3, c, cc}, {4, d, dd}, {6, f,
ff}, {7, g, gg}, {13, j, jj}};
list2 = {{1, 10, 100, 1000}, {2, 20, 200, 2000}, {5, 50, 500,
5000}, {6, 60, 600, 6000}, {7, 70, 700, 7000}, {9, 90, 900,
9000}};
I am trying to merge these lists using their keys. If the key doesn't match, one list should have missing values such as -99.99. The result I am looking for the above two list would be
answerlist = {{1, a, aa, 10, 100, 1000}, {2, b, bb, 20, 200,
2000}, {3, c, cc, -99.99, -99.99, -99.99}, {4, d,
dd, -99.99, -99.99, -99.99}, {5, -99.99, -99.99, 50, 500,
5000}, {6, f, ff, 60, 600, 6000}, {7, g, gg, 70, 700,
7000}, {9, -99.99, -99.99, 90, 900, 9000}, {13, j,
jj, -99.99, -99.99, -99.99}};
Thank you for your time and support in advance.
Answer
Another formulation:
merge1[a_List, b_List, pad_] :=
Module[{rules, keys},
rules = Apply[# -> {##2} &, {a, b}, {2}];
keys = Union @@ Keys @ rules;
Join[List /@ keys, ##, 2] & @@
(Lookup[#, keys, pad & /@ #[[1, 2]] ] & /@ rules)
]
Test:
merge1[list1, list2, -99.99] == answerlist
True
Comments
Post a Comment