2
$\begingroup$

I have been trying for sometime to achieve the following:- Given the list - {{"E", 3}, {"D", 2}, {"S", 2}, {"M", 2}} a list of pairs which maybe 4 pairs - more or less... This list is an output of a previous calculation - so the result can be different every calculation. I need the output list shown above to highlight in BOLD the pair with the highest value. Thus the result would look like - {{"E", 3}, {"D", 2}, {"S", 2}, {"M", 2}} in this case. I am sure the answer is so simple, but I can't find it. Please could anyone advise how to do this with coding. Many thanks for your help & suggestions.

$\endgroup$
6
  • 1
    $\begingroup$ highlightLargest[list_List,f_:Last]:=With[{max=First@TakeLargestBy[list,f,1]},Map[If[#===max,Style[#,Bold],#]&,list]] should do the trick but the output of this can not be used computationally anymore (unless one removes the Style wrapper from the largest element with e.g. a rule like Style[x_, ___] :> x. $\endgroup$
    – N0va
    May 3, 2022 at 7:22
  • 1
    $\begingroup$ I request you to take a look at the book written by the inventor; Chapter 7 specifically. $\endgroup$
    – Syed
    May 3, 2022 at 7:41
  • 1
    $\begingroup$ If you have your result in a variable called list, you can use MapAt[Style[#, Bold] &, list, OrderingBy[list, Last, -1]] $\endgroup$
    – Ben Izd
    May 3, 2022 at 8:21
  • $\begingroup$ Many thanks for everyone's help - I think this answer works best for my situation - MapAt[Style[#, Bold] &, list, OrderingBy[list, Last, -1]] Many thanks Ben Izd $\endgroup$ May 3, 2022 at 9:45
  • 1
    $\begingroup$ If @BenIzd doesn't post an answer, feel free to post your solution as an answer instead of a comment. $\endgroup$
    – Michael E2
    May 3, 2022 at 16:43

1 Answer 1

5
$\begingroup$

You can highlight the largest case with MapAt:

MapAt[Style[#, Bold] &, list, OrderingBy[list, Last, -1]]

In the last argument, I used OrderingBy to find the index of the largest element.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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