I have an association that maps {x, y}
pairs to values, for example:
<|{0, 0} -> "a", {-1, -4} -> "b", {-1, 1} -> "c", {-1, 0} -> "d", {1, 0} -> "e"|>
I want to convert this to a nested association with the outer association keyed by x
and the inner by y
:
<|0 -> <|0 -> "a"|>, -1 -> <|-4 -> "b", 0 -> "d", 1 -> "c"|>, 1 -> <|0 -> "e"|>|>
My current code is (assuming a
is the association):
Apply[Association]@*Map[Last@# -> a[#] &] /@ GroupBy[Keys@a, First, Sort]
but I'm wondering if there's a more elegant way to do this (sorting not necessary).
Association /@
rather thanApply[Association]@*
in your code with same results. $\endgroup$