Select elements from a list of list where the elements of the sublists meet a criteria

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}}



4 Answers

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}}

• 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]
– bmf
Commented Mar 30, 2022 at 20:43
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}}

• 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 :-) )
– bmf
Commented Mar 30, 2022 at 21:40
• thank you @bmf :)
– kglr
Commented Mar 31, 2022 at 0:47

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.}}

Using Merge and MapApply (new in 13.1)

Merge[Max] @ MapApply[{#1, #2} -> #3 &] @ data


<|{-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|>

Convert to list

KeyValueMap[Flatten @* List][%]


{{-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}}