3
$\begingroup$

I am not good at Mathematica. I have the following list already ordered according to the second part:

list1 = {{3, 2}, {5, 2}, {7, 2}, {7, 4}, {8, 4}, {12, 8}, {94, 8}, {4, 10}, {42, 10}};

I want to select the first elements where the second part gets bigger. The result would be:

list2 = {{3, 2}, {7, 4}, {12, 8}, {4, 10}}
$\endgroup$
2
$\begingroup$

One approach goes like this:

First /@ GatherBy[list1, Last]

It turns out that GatherBy can be replaced by SplitBy.

$\endgroup$
  • $\begingroup$ Wow! Thank you very much! $\endgroup$ – user57467 Dec 18 '18 at 14:16
  • $\begingroup$ @user57467 Glad to be helpful. $\endgroup$ – Αλέξανδρος Ζεγγ Dec 18 '18 at 14:24
1
$\begingroup$

You can also use SequenceReplace:

SequenceReplace[list1, {p : ({_, a_} ..)} :> First[{p}]]

{{3, 2}, {7, 4}, {12, 8}, {4, 10}}

$\endgroup$
  • $\begingroup$ This function is still not included in my version. I will study it in the future. Thank you very much! $\endgroup$ – user57467 Dec 19 '18 at 16:10

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.