I've been trying to figure out how to gather or group on a list of a nested associated--and flailing.
Here is a reduced example. I'm interested in performance as the is for a larger example and the operation will be repeated frequently.
Consider:
test = {
<|"me" -> <|"a" -> 1, "id" -> 2|>,
"friends" -> {<|"gender" -> 1, "id" -> 5|>} |>,
<|"me" -> <|"a" -> 2, "id" -> 6|>,
"friends" -> {<|"gender" -> 1, "id" -> 5|>, <|"gender" -> 2,
"id" -> 12|>, <|"gender" -> 3, "id" -> 13|>} |>,
<|"me" -> <|"a" -> 3, "id" -> 8|>,
"friends" -> {<|"gender" -> 3, "id" -> 13|>} |>
}
I want to collect all "me" associations that have the same "friends" "id" together. For example, for the above:
{
<|"gender" -> 1,
"id" -> 5|> -> {<|"a" -> 1, "id" -> 2|>, <|"a" -> 2, "id" -> 6|>},
<|"gender" -> 2, "id" -> 12|> -> { <|"a" -> 2, "id" -> 6|>},
<|"gender" -> 3,
"id" -> 13|> -> {<|"a" -> 2, "id" -> 6|>, , <|"a" -> 3,
"id" -> 8|>}
}
I've tried many things such as Gather[test, #1[["friends", "id"]] == #2[["friends", "id"]] &]
and some attempts a Sow/Reap.
Can anyone see how to achieve this?
Normal@Merge[Table[friend -> #me, {friend, #friends}] & /@ test, Identity]
$\endgroup$