Hello, I am using the KeyUnion function to combine a couple of lists of lists. In some cases, a key exists in one list of sublists, but not in the other.
Here is an example:
assoc1 = <|1 -> {1, a, aa},3 -> {3, c, cc}|>
assoc2 = <|1 -> {1, 10, 100, 1000}, 5 -> {5, 50, 500, 5000}|>
I do a KeyUnion…
assocList = KeyUnion[{assoc1, assoc2}]
…to get something like this…
assocList = { <|1 -> {1, a, aa},3 -> {3, c, cc}, 5 -> Missing["KeyAbsent", 5]|>, <|1 -> {1, 10, 100, 1000},
3 -> Missing["KeyAbsent", 3], 5 -> {5, 50, 500, 5000}|>}
Now I want to merge the two lists, based on the key, replacing the missing values by some Key, Value pair that includes “dummy” value such as 5->{-9999}. The following piece of code, for example, works nicely for an individual replacement (eg. Key “3”):
Merge[{assocList[[1]], assocList[[2]]}, Identity]
merged = Values[%] /. _Missing -> {3, -9999};
To get…
{{{1, a, aa}, {1, 10, 100, 1000}}, {{3, c, cc}, {3, -9999}},...}
But you also get…
{...,{{3,-9999}, {5, 50, 500, 5000}},...}
Which is not what we want. (The ultimate goal is to make a Union between the two lists associated with each key, where the first item in the list is also the same as the key.)
What I am looking for is a way to make a {Key, Value} pair replacement for each missing key. Is there a way to Is there a way to pick out the “index” part of the Missing[KeyAbsent, index] so that I can write a replacement rule that iterates over the keys?