7
$\begingroup$

Let's say that I have the following list

data = {{-41.10000000000002`, 2.873303230466569`, 
    6.310063685614807`}, {-41.10000000000002`, 2.873303230466569`, 
    5.839065774185954`}, {-41.10000000000002`, 19.236823144577595`, 
    11.735697959768771`}, {-41.10000000000002`, 19.236823144577595`, 
    12.10072134112616`}, {-31.340000000000032`, 11.842619292783251`, 
    20.776241204139154`}, {-31.340000000000032`, 11.842619292783251`, 
    20.78670782439312`}, {-21.590000000000032`, 4.449371441148591`, 
    19.155223392304862`}, {-21.590000000000032`, 4.449371441148591`, 
    19.363247469852638`}, {-41.10000000000002`, 35.60127021988911`, 
    3.921057612645157`}, {-41.10000000000002`, 35.60127021988911`, 
    9.843856348862907`}};

I would like to select the sublists with the same first and second element. For all sublist that meet this first criteria, select the maximum third element. For example for the given list, the solution will be:

{{-41.10000000000002`, 2.873303230466569`, 
  6.310063685614807`}, {-41.10000000000002`, 19.236823144577595`, 
  12.10072134112616`}, {-31.340000000000032`, 11.842619292783251`, 
  20.78670782439312`}, {-21.590000000000032`, 4.449371441148591`, 
  19.363247469852638`}, {-41.10000000000002`, 35.60127021988911`, 
  9.843856348862907`}}

$\endgroup$
0

3 Answers 3

8
$\begingroup$
Last /@ (SortBy[#, Last] & /@ GatherBy[data, {#[[1]], #[[2]]} &])
{{-41.1, 2.8733, 6.31006}, {-41.1, 19.2368, 12.1007}, {-31.34, 
  11.8426, 20.7867}, {-21.59, 4.44937, 19.3632}, {-41.1, 35.6013, 
  9.84386}}
$\endgroup$
1
  • $\begingroup$ Very nice. Unfortunately I have to wait until I can vote again. Just a quick comment. Someone left a comment under my answer that was meant for yours. You can replace the GatherBy by GatherBy[data, Most] $\endgroup$
    – bmf
    Mar 30, 2022 at 20:43
7
$\begingroup$
Values @ GroupBy[data, Most, First @* MaximalBy[Last]]
{{-41.1, 2.8733, 6.31006},
 {-41.1, 19.2368, 12.1007}, 
 {-31.34, 11.8426, 20.7867}, 
 {-21.59, 4.44937, 19.3632},
 {-41.1, 35.6013, 9.84386}}
$\endgroup$
2
  • 1
    $\begingroup$ It's gonna be a (+1) from once I get to vote again. I just wanted to say that looking at your solution, I feel stupid. And more importantly, every time I see you wrote an answer I bring this in mind. (I hope you are not offended, I meant it as a good-hearted joke :-) ) $\endgroup$
    – bmf
    Mar 30, 2022 at 21:40
  • 1
    $\begingroup$ thank you @bmf :) $\endgroup$
    – kglr
    Mar 31, 2022 at 0:47
5
$\begingroup$

Data from the OP


data = {
   {-41.10000000000002`, 2.873303230466569`, 
    6.310063685614807`}, {-41.10000000000002`, 2.873303230466569`, 
    5.839065774185954`}, {-41.10000000000002`, 19.236823144577595`, 
    11.735697959768771`}, {-41.10000000000002`, 19.236823144577595`, 
    12.10072134112616`}, {-31.340000000000032`, 11.842619292783251`, 
    20.776241204139154`}, {-31.340000000000032`, 11.842619292783251`, 
    20.78670782439312`}, {-21.590000000000032`, 4.449371441148591`, 
    19.155223392304862`}, {-21.590000000000032`, 4.449371441148591`, 
    19.363247469852638`}, {-41.10000000000002`, 35.60127021988911`, 
    3.921057612645157`}, {-41.10000000000002`, 35.60127021988911`, 
    9.843856348862907`}
   };

The desired output as shown in the OP

opoutput = {
   {-41.10000000000002`, 2.873303230466569`, 
    6.310063685614807`}, {-41.10000000000002`, 19.236823144577595`, 
    12.10072134112616`}, {-31.340000000000032`, 11.842619292783251`, 
    20.78670782439312`}, {-21.590000000000032`, 4.449371441148591`, 
    19.363247469852638`}, {-41.10000000000002`, 35.60127021988911`, 
    9.843856348862907`}
   };

Proposed solution:


ArrayReshape[
 Table[Reverse /@ 
       Take[#, Ordering[#, -1]] &@(Reverse /@ #) &@{data[[xx]], 
    data[[xx + 1]]}, {xx, 1, Length@data, 2}], {Length@data/2, 3}]

Testing the proposed solution:


ArrayReshape[
  Table[Reverse /@ 
        Take[#, Ordering[#, -1]] &@(Reverse /@ #) &@{data[[xx]], 
     data[[xx + 1]]}, {xx, 1, Length@data, 2}], {Length@data/2, 
   3}] - opoutput

{{0., 0., 0.}, {0., 0., 0.}, {0., 0., 0.}, {0., 0., 0.}, {0., 0., 0.}}

$\endgroup$
0

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.