TakeSmallestBy
TakeSmallestBy[list,f,n]
gives the n elements ei in list for which f[ei] is smallest, sorted in ascending order.
TakeSmallestBy[listprop,f,n]
gives the property prop for the n elements in list for which f[ei] is smallest.
TakeSmallestBy[f,n]
represents an operator form of TakeSmallestBy that can be applied to an expression.
Details and Options
- TakeSmallestBy sorts the f[ei] by numerical magnitude.
- It handles numeric objects with real values, Quantity objects, and DateObject constructs.
- TakeSmallestBy expects all the f[ei] to be comparable with one another.
- In TakeSmallestBy[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 All gives an association with element, index and f value - TakeSmallestBy has option ExcludedForms. With the default setting ExcludedForms->Automatic, TakeSmallestBy drops 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 TakeSmallestBy.
- TakeSmallestBy[list,f,UpTo[n]] takes n elements, or as many as are available.
- TakeSmallestBy[assoc,f,n] gives an association of length n by taking the values in assoc that are smallest according to f, preserving their keys.
- TakeSmallestBy[f,n][list] is equivalent to TakeSmallestBy[list,f,n].
Examples
open allclose allBasic Examples (3)
Take the two shortest strings in a list:
Do the same using the operator form of TakeSmallestBy:
Take the two shortest strings in an association:
Give the four numbers closest to zero:
Give the four numbers closest to zero, or as many as are available if fewer:
Scope (3)
Get the two smallest elements by magnitude in a list:
Get the positions of the two smallest elements by magnitude in a list:
Get the two smallest elements by magnitude in a list along with their positions:
Get the two smallest elements by magnitude in a list with their positions given first:
Get associations containing the element, position and Abs value of the two smallest elements by magnitude in a list:
Find the 10 countries whose names have the lowest total of letter numbers:
Properties & Relations (1)
TakeSmallestBy[{e1,e2,…},f,n] compares values f[ei] using NumericalOrder:
MinimalBy[{e1,e2,…},f,n] compares values f[ei] using canonical Order:
Possible Issues (2)
If fewer than the requested number of elements are present, TakeSmallestBy will not evaluate:
If the f[ei] are not comparable, TakeSmallestBy will not evaluate:
Text
Wolfram Research (2015), TakeSmallestBy, Wolfram Language function, https://reference.wolfram.com/language/ref/TakeSmallestBy.html (updated 2020).
CMS
Wolfram Language. 2015. "TakeSmallestBy." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/TakeSmallestBy.html.
APA
Wolfram Language. (2015). TakeSmallestBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TakeSmallestBy.html