4
$\begingroup$

For example, if i have a list as following:

 example = {
  {{a, b, 3}, {c, d, 2}, {e, f, 1}},
  {{a, b, 5}, {c, d, 2}, {e, f, 8}},
  {{a, b, 4}, {c, d, 3}, {e, f, 2}},
  {{a, b, 1}, {c, d, 2}, {e, f, 4}}
  }

How to create a new list which join the subset which has the largest 3rd element? for example, in the first line: {{a, b, 3}, {c, d, 2}, {e, f, 1}} “3” is the largest number at the third position of each subset, so i only extract {a, b, 3} Eventually, i will get a new list as following:

 newset = {
  {a, b, 3},
  {e, f, 8},
  {a, b, 4},
  {e, f, 4}
  }

so how to achieve this?

$\endgroup$
7
$\begingroup$
First@MaximalBy[#, Last] & /@ example
{{a, b, 3}, {e, f, 8}, {a, b, 4}, {e, f, 4}}
$\endgroup$
4
$\begingroup$
Map[Last @* SortBy[Last]] @ example
 {{a, b, 3}, {e, f, 8}, {a, b, 4}, {e, f, 4}}

Also

#[[First @ Ordering[Last /@ #, -1]]] & /@ example
{{a, b, 3}, {e, f, 8}, {a, b, 4}, {e, f, 4}}
$\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.