6
$\begingroup$

Mathematica shows my data in the below style:

  lst={....
 {-0.410927, {{11, 16}, {12, 16}, {15, 5}, {15, 6}}},
 {-0.410927, {{9, 14}, {10, 14}, {13, 7}, {13, 8}}}
  .....}

As I copy the first -0.410927 in another cell, I saw that it is really -0.41092720756334744 and for the second -0.410927, I noticed that it is -0.4109272075633472 in fact. Regards to the other rows in the above and the below positions of these two shown rows, which are presented by ...., how I merge such two rows and have a result as

output=lst={....
 {-0.410927, {{11, 16}, {12, 16}, {15, 5}, {15, 6}, {9, 14}, {10, 14}, {13, 7}, {13, 8}}}
  .....}

In fact, there is no any differences between two -0.410927for more digits than 3.

I have to apply this procedure for other rows considering this fact that all elements have just 3 digits for being important. For instance 1.34267 is not different from 1.34311.

How can I help Mathematica to merge multi rows with very similar first elements. The similarity is gained after rounding values up to 3 digits.

$\endgroup$
6
$\begingroup$
lst = { {-0.410927, {{11, 16}, {12, 16}, {15, 5}, {15, 6}}},
   {-0.41, {{11, 16}, {12, 16}, {15, 5}, {15, 6}}},
    {-0.45123, {{11, 16}, {12, 16}, {15, 5}, {15, 6}}},
 {-0.410927, {{9, 14}, {10, 14}, {13, 7}, {13, 8}}}};

Values@GroupBy[lst,  Round[First@#, .001] &, {#[[1, 1]], Join @@ #[[All, 2]]} &]

{{-0.410927, {{11, 16}, {12, 16}, {15, 5}, {15, 6}, {9, 14}, {10,     14}, {13, 7}, {13, 8}}},
{-0.41, {{11, 16}, {12, 16}, {15,     5}, {15, 6}}},
{-0.45123, {{11, 16}, {12, 16}, {15, 5}, {15, 6}}}}

Values@GroupBy[lst,  Round[First@#, .01] &, {#[[1, 1]], Join @@ #[[All, 2]]} &]

{{-0.410927, {{11, 16}, {12, 16}, {15, 5}, {15, 6}, {11, 16}, {12, 16}, {15, 5}, {15, 6}, {9, 14}, {10, 14}, {13, 7}, {13, 8}}},
{-0.45123, {{11, 16}, {12, 16}, {15, 5}, {15, 6}}}}

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thankxx so much. But a problem: once I write the result as grouped[[1]] I reach {{-0.410927, {{11, 16}, {12, 16}, {15, 5}, {15, 6}}}, {-0.410927, {{9,14}, {10, 14}, {13, 7}, {13, 8}}}} this out put is not yet merged! $\endgroup$ – Inzo Babaria May 16 '19 at 21:24
  • $\begingroup$ @Inzo, please see the updated version. $\endgroup$ – kglr May 16 '19 at 21:34
  • $\begingroup$ Amazing and wonderful !!! $\endgroup$ – Inzo Babaria May 16 '19 at 23:49
3
$\begingroup$
lst={{-0.410927,{{11,16},{12,16},{15,5},{15,6}}},{-0.41,{{11,16},{12,16},{15,5},{15,6}}},{-0.45123,{{11,16},{12,16},{15,5},{15,6}}},{-0.410927,{{9,14},{10,14},{13,7},{13,8}}}};

{#[[1,1]],Flatten[#[[All,2]],1]}&/@Values[GroupBy[lst,SetPrecision[#[[1]],3]&]]   

{{-0.410927, {{11, 16}, {12, 16}, {15, 5}, {15, 6}, {9, 14}, {10, 14}, {13, 7}, {13, 8}}}, {-0.41, {{11, 16}, {12, 16}, {15, 5}, {15, 6}}}, {-0.45123, {{11, 16}, {12, 16}, {15, 5}, {15, 6}}}}

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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