TakeLargestBy

TakeLargestBy[list,f,n]

gives the n elements ei in list for which f[ei] is largest, sorted in descending order.

TakeLargestBy[listprop,f,n]

gives the property prop for the n elements in list for which f[ei] is largest.

TakeLargestBy[f,n]

represents an operator form of TakeLargestBy that can be applied to an expression.

Details and Options

  • TakeLargestBy sorts the f[ei] by numerical magnitude.
  • It handles numeric objects with real values, Quantity objects, and DateObject constructs.
  • TakeLargestBy expects all the f[ei] to be comparable with one another.
  • In TakeLargestBy[listprop,f,n], possible forms for prop include:
  • "Element"gives each element itself
    "Index"gives the index for each element
    "Value"gives the value f[x] for each element x
    {prop1,prop2,}a list of multiple forms
    Allgives an association with element, index and f value
  • TakeLargestBy has option ExcludedForms. With the default setting ExcludedForms->Automatic, TakeLargestBy excludes from its final results elements for which f[ei] is None, Null, or Indeterminate, or has head Missing.
  • The setting ExcludedForms->{p1,p2,} specifies that expressions for which f[ei] matches any of the pi should be excluded from results generated by TakeLargestBy.
  • TakeLargestBy[list,f,UpTo[n]] takes n elements, or as many as are available.
  • TakeLargestBy[assoc,f,n] gives an association of length n by taking the values in assoc that are largest according to f, preserving their keys.
  • TakeLargestBy[f,n][list] is equivalent to TakeLargestBy[list,f,n].

Examples

open allclose all

Basic Examples  (3)

Take the two longest strings in a list:

Do the same using the operator form of TakeLargestBy:

Take the two longest strings in an association:

Give the four numbers farthest from zero:

Give the four numbers farthest from zero, or as many as are available if fewer:

Scope  (5)

Get the two largest elements by magnitude in a list:

Get the positions of the two largest elements by magnitude in a list:

Get the two largest elements by magnitude in a list along with their positions:

Get the two largest elements by magnitude in a list with their positions given first:

Get associations containing the element, position and Abs value of the two largest elements by magnitude in a list:

Find the three capitals of continental US states that are farthest from Kansas City:

Find the five most recent James Bond movies:

Find the 15 countries with the longest names:

Applications  (1)

Find the four US states with the largest number of neighboring states, and show them:

Properties & Relations  (1)

TakeLargestBy[{e1,e2,},f,n] compares values f[ei] using NumericalOrder:

MaximalBy[{e1,e2,},f,n] compares values f[ei] using canonical Order:

Possible Issues  (2)

If fewer than the requested number of elements are present, TakeLargestBy will not evaluate:

If the f[ei] are not comparable, TakeLargestBy will not evaluate:

Neat Examples  (1)

From the BRICS group of countries, give the country that is farthest from the current location:

Wolfram Research (2015), TakeLargestBy, Wolfram Language function, https://reference.wolfram.com/language/ref/TakeLargestBy.html (updated 2020).

Text

Wolfram Research (2015), TakeLargestBy, Wolfram Language function, https://reference.wolfram.com/language/ref/TakeLargestBy.html (updated 2020).

CMS

Wolfram Language. 2015. "TakeLargestBy." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/TakeLargestBy.html.

APA

Wolfram Language. (2015). TakeLargestBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TakeLargestBy.html

BibTeX

@misc{reference.wolfram_2024_takelargestby, author="Wolfram Research", title="{TakeLargestBy}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/TakeLargestBy.html}", note=[Accessed: 08-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_takelargestby, organization={Wolfram Research}, title={TakeLargestBy}, year={2020}, url={https://reference.wolfram.com/language/ref/TakeLargestBy.html}, note=[Accessed: 08-December-2024 ]}