Given list of associations, generate a list of all common associations
gather[x_] := {#[[1, 1]], #[[All, -1]]}& /@ Join[GatherBy[x, First],
GatherBy[{#[[All, 1]], #[[1, -1]]}&/@ Select[GatherBy[x, Last], Length@# >1&], First]] /.
{{} -> Sequence[], {a_, {y_}} :> {a, y}}
gather @ list
{{AA, 3}, {AB, {2, 4}}, {BA, {2, 5}}, {BB, 6}, {{AB, BA}, 2}}
gather @ list2
{{{15, "->", 15}, {3, 8}}, {{16, "->", 16}, {2, 3, 4, 5, 6, 7, 8}}, {{{15, "->", 15}, {16, "->", 16}}, {3, 8}}}
Here is an alternative approach. It relies on Merge
. It works with the example, but I'm not sure it's fully general.
It's convenient to start with a list of rules:
rlist = Rule @@@ list
Here are the functions used in the merges:
fun1 = If[Length[#] == 1, #[[1]], #] &
fun2 = Sort[#][[-1]] &
Here's the main part:
List @@@
Normal @
Merge[{
Normal @ Merge[rlist, fun1],
Reverse /@ Normal @ Merge[Reverse /@ rlist, fun1]
},
fun2
]
list = {{AA, 3}, {AB, 2}, {AB, 4}, {BA, 2}, {BA, 5}, {BB, 6}};
list2 = {{{15, "->", 15}, 3}, {{15, "->", 15}, 8}, {{16, "->", 16},
2}, {{16, "->", 16}, 3}, {{16, "->", 16}, 4}, {{16, "->", 16},
5}, {{16, "->", 16}, 6}, {{16, "->", 16}, 7}, {{16, "->", 16}, 8}};
list3 = {{{1, "->", 1}, 8}, {{3, "->", 3}, 8}, {{10, "->", 10},
8}, {{12, "->", 12}, 8}, {{13, "->", 13}, 2}, {{13, "->", 13},
7}, {{13, "->", 13}, 8}, {{14, "->", 14}, 6}, {{14, "->", 14},
8}, {{15, "->", 15}, 3}, {{15, "->", 15}, 8}, {{16, "->", 16},
2}, {{16, "->", 16}, 3}, {{16, "->", 16}, 4}, {{16, "->", 16},
5}, {{16, "->", 16}, 6}, {{16, "->", 16}, 7}, {{16, "->", 16},
8}};
fun[lista_List] := Module[{as1, as4,as5}
,
as1 = GroupBy[lista, First -> Last];
as4 = Select[Reverse@GroupBy[lista, Last -> First], Length[#] > 1 &];
as5 = Merge[Association[Thread[Subsets[Lookup[as4, #], {2}] -> #]] & /@ Keys@as4, Identity];
{#[[1]], Sort[#[[2]]]} & /@ Transpose[{Keys[#], List @@ Normal @@@ #}] &@ Merge[{as1, as5}, Identity]]
fun[list2]
gives me :
{{15, "->", 15}, {3, 8}}, {{16, "->", 16}, {2, 3, 4, 5, 6, 7, 8}}, {{{15, "->", 15}, {16, "->", 16}}, {3,8}}}
while
Grid@fun[list3]
gives me:
{{{1, "->", 1}, {8}}, {{3, "->", 3}, {8}}, {{10, "->", 10}, {8}}, {{12, "->", 12}, {8}}, {{13, "->", 13}, {2, 7, 8}}, {{14, "->", 14}, {6, 8}}, {{15, "->", 15}, {3, 8}}, {{16, "->", 16}, {2, 3, 4, 5, 6, 7, 8}}, {{{15, "->", 15}, {16, "->", 16}}, {3, 8}}, {{{14, "->", 14}, {16, "->", 16}}, {6, 8}}, {{{13, "->", 13}, {16, "->", 16}}, {2, 7, 8}}, {{{1, "->", 1}, {3, "->", 3}}, {8}}, {{{1, "->", 1}, {10, "->", 10}}, {8}}, {{{1, "->", 1}, {12, "->", 12}}, {8}}, {{{1, "->", 1}, {13, "->", 13}}, {8}}, {{{1, "->", 1}, {14, "->", 14}}, {8}}, {{{1, "->", 1}, {15, "->", 15}}, {8}}, {{{1, "->", 1}, {16, "->", 16}}, {8}}, {{{3, "->", 3}, {10, "->", 10}}, {8}}, {{{3, "->", 3}, {12, "->", 12}}, {8}}, {{{3, "->", 3}, {13, "->", 13}}, {8}}, {{{3, "->", 3}, {14, "->", 14}}, {8}}, {{{3, "->", 3}, {15, "->", 15}}, {8}}, {{{3, "->", 3}, {16, "->", 16}}, {8}}, {{{10, "->", 10}, {12, "->", 12}}, {8}}, {{{10, "->", 10}, {13, "->", 13}}, {8}}, {{{10, "->", 10}, {14, "->", 14}}, {8}}, {{{10, "->", 10}, {15, "->", 15}}, {8}}, {{{10, "->", 10}, {16, "->", 16}}, {8}}, {{{12, "->", 12}, {13, "->", 13}}, {8}}, {{{12, "->", 12}, {14, "->", 14}}, {8}}, {{{12, "->", 12}, {15, "->", 15}}, {8}}, {{{12, "->", 12}, {16, "->", 16}}, {8}}, {{{13, "->", 13}, {14, "->", 14}}, {8}}, {{{13, "->", 13}, {15, "->", 15}}, {8}}, {{{14, "->", 14}, {15, "->", 15}}, {8}}}