Removing duplicate rows and appending from a table of lists
You can use GroupBy
and KeyValueMap
:
x3 = KeyValueMap[Join[{#}, ## & @@ Reverse@SortBy[First][#2]] &]@
GroupBy[x, First -> Rest]
Also
x4 = KeyValueMap[Join[{#}, #2] &]@
GroupBy[x, First -> Rest, Flatten[Reverse@SortBy[First]@#, 1] &]
x2 == x3 == x4
True
rule=Merge[Cases[
Reverse@x, {w_,{y__},{z__}} :> w-> {{y},{z}}],Catenate]
And:
(Join[{#},#/.rule]&/@DeleteDuplicates[x[[All,1]]])==x2
True
If there is no good reason why {3, 6, 10, 3, 6}
, should appear before {3, 6, 9, 3, 6}
in the merged list (x2
), change Reverse@x
to x
, but perhaps I am missing something important here?
Original Post
rule_alt=Catenate/@Merge[Cases[
Reverse@x, {w_,{y__},{z__}} :> w-> {{y},{z}}],Join]