TakeSmallestBy

TakeSmallestBy[data,f,n]

gives the n elements ei in data for which f[ei] is smallest, sorted in ascending order.

TakeSmallestBy[dataprop,f,n]

gives the property prop for the n elements in data for which f[ei] is smallest.

TakeSmallestBy[data,f,n,p]

uses the ordering function p for sorting.

TakeSmallestBy[f,n]

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

更多信息和选项

  • TakeSmallestBy gives the elements ei in the first level of the input data corresponding to the smallest f[ei] values.
  • By default, TakeSmallestBy uses NumericalOrder to sort f[ei] values that are numeric expressions, Quantity objects and DateObject expressions. For other types of objects, TakeSmallestBy sorts the f[ei] values by canonical Order.
  • The data can have the following forms:
  • {e1,e2,}list of values, including numbers, quantities, dates, ...
    Association[]association of values »
    QuantityArray[]quantity array or other structured array
    Tabular[]type-consistent tabular data »
    TabularColumn[]type-consistent column data »
    Dataset[]general hierarchical data »
  • 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.
  • For tabular data tab, TakeSmallestBy[tab,f,] applies the function f to individual rows of tab, with the row being an association <|col1val1,|> if tab has column keys or a list {val1,} if tab does not have column keys.
  • In TakeSmallestBy[dataprop,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 »
  • TakeSmallestBy[data,f,UpTo[n]] takes n elements or as many as are available. »
  • TakeSmallestBy[f,n][data] is equivalent to TakeSmallestBy[data,f,n].
  • TakeSmallestBy has option ExcludedForms. With the default setting ExcludedForms->Automatic, TakeSmallestBy excludes from its final results elements for which f[ei] is None, Null or Indeterminate or has head Missing. »
  • The setting ExcludedForms->{patt1,patt2,} specifies that expressions for which f[ei] matches any of the patti should be excluded from results generated by TakeSmallestBy. »

范例

打开所有单元关闭所有单元

基本范例  (5)

Give the four numbers closest to zero:

给出列表中两个最短的字符串:

利用 TakeSmallestBy 的运算符形式完成同样计算:

Take three smallest Quantity lengths according to their distance to :

Take the three DateObject expressions with the smallest day of the month:

Take the two shortest strings in an association:

范围  (8)

Give the four numbers closest to zero or as many as are available if fewer:

The discovery date value for some planets is missing:

By default, Missing[] and several other symbolic expressions are excluded if returned by the selection function:

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 magnitudes 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:

Take a few English words:

They can be translated to Spanish with WordTranslation, which gives a list of possibilities:

These are the smallest two words according to canonical order:

These are the smallest two English words according to canonical order of their Spanish translations:

Use properties to show simultaneously the English word and its translation to Spanish:

Construct a TabularColumn object with 100 words:

Select the five shortest words:

Normalize the result to a list:

Find the four rows in a Tabular object with smallest values in a given column:

Use general functional notation instead of the column name:

Take a dataset of the solar system planets:

Find the three planets with the smallest number of moons:

Take a list of integer 3-vectors:

These are their norms:

Select the three vectors with smallest norms, by default sorting numerically those norms:

Sorting the norms by canonical order can give a different result:

选项  (2)

ExcludedForms  (2)

Take the list of all planets in the solar system:

Find the four planets that are currently closest to your location, with a lower limit of .1 au in distance (to exclude the location planet):

Plot the evolution of the distances during a two-year period centered at the current moment:

Take an association:

Not all these keys have values in the association:

Find the keys corresponding up to the four smallest values in the association:

By default, some symbolic objects like Missing[] or None are excluded as results of the selection function, so the previous result is equivalent to this:

Specify that no value should be excluded:

Specify that only values None should be excluded:

应用  (6)

Find the eight capitals of continental US states that are closest to Kansas City:

Find the five oldest James Bond movies:

Find the 10 countries with the shortest names:

From the BRICS group of countries, give the country that is closest to the current location:

Find the seven US states with the smallest number of neighboring states and show them:

Find the five constellations with the smallest number of bright stars, showing how many each one has:

属性和关系  (3)

TakeSmallestBy[list,f,n,p] is effectively equivalent to Part[list,TakeSmallest[Map[f,list]"index",n,p]:

TakeSmallestBy[{e1,e2,},f,n] compares values f[ei] using NumericalOrder by default:

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

Both TakeSmallestBy and MinimalBy take an ordering function as fourth argument, which makes them effectively equivalent:

When there are common values of f[ei] for different elements ei in TakeSmallestBy[{e1,e2,},f,n], the original order will be kept:

可能存在的问题  (2)

如果列表中的元素少于所要求的元素个数,不执行 TakeSmallestBy 运算:

Use UpTo to get as many elements as possible:

如果无法比较 f[ei] 的值,不执行 TakeSmallestBy 运算:

Wolfram Research (2015),TakeSmallestBy,Wolfram 语言函数,https://reference.wolfram.com/language/ref/TakeSmallestBy.html (更新于 2025 年).

文本

Wolfram Research (2015),TakeSmallestBy,Wolfram 语言函数,https://reference.wolfram.com/language/ref/TakeSmallestBy.html (更新于 2025 年).

CMS

Wolfram 语言. 2015. "TakeSmallestBy." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2025. https://reference.wolfram.com/language/ref/TakeSmallestBy.html.

APA

Wolfram 语言. (2015). TakeSmallestBy. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/TakeSmallestBy.html 年

BibTeX

@misc{reference.wolfram_2025_takesmallestby, author="Wolfram Research", title="{TakeSmallestBy}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/TakeSmallestBy.html}", note=[Accessed: 07-June-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_takesmallestby, organization={Wolfram Research}, title={TakeSmallestBy}, year={2025}, url={https://reference.wolfram.com/language/ref/TakeSmallestBy.html}, note=[Accessed: 07-June-2025 ]}