7
$\begingroup$

For the table x with three lists per row:

x={{{10, 56, 528, 6240, 97920, 1751040, 38154240, 1058365440, 
    31677972480, 1134275788800, 45248407142400}, {2, 14, 15, 1, 
    2}, {1}}, {{3, 60, 280, 1232, 192192, 52276224, 17878468608, 
    74764505088, 1238954655744, 1152227829841920, 
    1534767469349437440}, {3, 6, 9, 3, 6}, {1}}, {{3, 60, 280, 1232, 
    192192, 52276224, 17878468608, 74764505088, 1238954655744, 
    1152227829841920, 1534767469349437440}, {3, 6, 10, 3, 6}, {1}}};

How can I convert the above table to this:

x2={{{10,56,528,6240,97920,1751040,38154240,1058365440,31677972480,1134275788800,45248407142400},{2,14,15,1,2},{1}},
{{3,60,280,1232,192192,52276224,17878468608,74764505088,1238954655744,1152227829841920,1534767469349437440},{3,6,10,3,6},{1},{3,6,9,3,6},{1}}};

Ie duplicates x[[n]][[1]] are removed and then for each duplicate n, x[[n]][[2]] and x[[n]][[3]] are appended to the row for the distinct n.

Also for x, how can the length be checked for how many lists are in x[[n]][[1]]? Is there a way to directly remove duplicate n's?

Thanks.

$\endgroup$

2 Answers 2

10
$\begingroup$

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

$\endgroup$
2
$\begingroup$
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]   
$\endgroup$
1
  • 1
    $\begingroup$ The ordering doesn't matter in this case, but I didn't notice I reversed it actually! $\endgroup$
    – Jamie M
    Feb 20, 2020 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.