SelectFirst[data,crit]
给出 data 中 crit[ei] 为 True 的第一个 ei,如果没有找到,则给出 Missing["NotFound"].
SelectFirst[data,critprop]
返回所选的元素的属性 prop.
SelectFirst[data,crit,default]
如果 data 中没有使 crit[ei] 为 True 的 ei,则给出 default.
SelectFirst[crit]
表示可应用于表达式的 SelectFirst 的算符形式.
SelectFirst
SelectFirst[data,crit]
给出 data 中 crit[ei] 为 True 的第一个 ei,如果没有找到,则给出 Missing["NotFound"].
SelectFirst[data,critprop]
返回所选的元素的属性 prop.
SelectFirst[data,crit,default]
如果 data 中没有使 crit[ei] 为 True 的 ei,则给出 default.
SelectFirst[crit]
表示可应用于表达式的 SelectFirst 的算符形式.
更多信息
- SelectFirst 保留使给定条件 crit 为 True 的第一个元素.
- SelectFirst[data,crit] 等价于 First[Select[data,crit]].
- data 可采用以下形式和解释:
-
{e1,e2,…} 值的列表 » f[e1,e2,…] 任意标头 f » Association 值的关联 » QuantityArray[…] 数量数组或其他结构化数组 Tabular[…] 类型一致的表格数据 » TabularColumn[…] 类型一致的列数据 » Dataset[…] 普通的层次化数据 - 属性 prop 可采用以下形式和解释:
-
"Element" 选中的值 » "Index" 选中的值的索引 » "BitVectorMask" 布尔掩码,对选定的值返回 True,否则返回 False » {prop1,prop2,…} 多种形式组成的列表 » All 给出元素、索引和位向量掩码的关联 » - 在 SelectFirst[data,crit,default] 中,只有在 data 中没有使 crit[ei] 为 True 的 ei,才计算 default.
- SelectFirst[crit][data] 等价于 SelectFirst[data,crit].
范例
打开所有单元 关闭所有单元基本范例 (6)
SelectFirst[{1, 2, 4, 7, 6, 2}, EvenQ]SelectFirst[{1, 2, 4, 7, 6, 2}, # > 2& -> "Index"]SelectFirst[{1, 3, 5}, EvenQ, x]使用 SelectFirst 的算符形式:
SelectFirst[EvenQ][{1, 2, 4, 7, 6, 2}]SelectFirst 作用于 Association 中的数值上:
SelectFirst[<|a -> 1, b -> 2, c -> 3, d -> 4|>, # > 2&]对 Tabular 对象使用 SelectFirst:
Tabular[{{"cat", 2}, {"fox", 3}, {"dog", 5}}, {"a", "b"}]SelectFirst[%, OddQ[#b]&]Normal[%]范围 (15)
基本用法 (5)
SelectFirst[{{1, y}, {2, z}, {3, x}, {4, y}, {5, x}}, MemberQ[#, x]&]SelectFirst[{1, 2, 4, 7, 6, 2}, # > 2&]SelectFirst[Range[10], GreaterThan[3]]使用算符形式的 SelectFirst:
Range[10]//SelectFirst[GreaterThan[3]]SelectFirst[{1, 2, 3}, StringQ, "NoStrings"]输入数据 (5)
SelectFirst 适用于任何标头,不只是 List:
SelectFirst[f[1, a, 2, b, 3], IntegerQ]SelectFirst 适用于 Association 中的数值:
SelectFirst[<|"a" -> {.1, .3, .2}, "b" -> {"dog", "cat", "fox"}|>, AllTrue[#, StringQ]&]SelectFirst 适用于 SparseArray 对象:
s = SparseArray[Table[2 ^ i -> i, {i, 0, 5}]]SelectFirst[s, OddQ]SelectFirst 适用于 TabularColumn 对象:
TabularColumn[RandomDate[10 ^ 5]]SelectFirst[SameQ[DateValue[#, "Month"], 11]&][%]对含有命名列的 Tabular 对象应用 SelectFirst:
tab = ToTabular[{"a" -> {1, 2, 3}, "b" -> {.1, .3, .2}, "c" -> {"dog", "cat", "fox"}, "d" -> {DateObject[{2024, 8, 1}], DateObject[{2024, 8, 2}], DateObject[{2024, 8, 3}]}}, "Columns"]SelectFirst[tab, Function[BusinessDayQ[#d]]]Normal[%]属性的形式 (5)
SelectFirst[{2, 3, 4, 5}, OddQ -> "Element"]SelectFirst[{2, 3, 4, 5}, OddQ -> "Index"]SelectFirst[{2, 3, 4, 5}, OddQ -> "BitVectorMask"]Normal[%]SelectFirst[{2, 3, 4, 5}, OddQ -> {"Element", "Index"}]SelectFirst[{2, 3, 4, 5}, OddQ -> All]应用 (7)
选择 9 到 99 之间第一个模 3 和 模 5 都等于 1 的数字:
SelectFirst[Range[9, 99], Mod[#, 3] == 1 && Mod[#, 5] == 1&]SelectFirst[Tuples[{a, b}, 4], # == Reverse[#]&]求第一个行列式为 1 的由 0 和 1 组成的 3×3 矩阵:
SelectFirst[Tuples[{0, 1}, {3, 3}], Det[#] == 1&]SelectFirst[Eigenvalues[RandomReal[1, {5, 5}]], Abs[#] < 1&]求第一个名称长度至少为 36 个字符的内置 Wolfram 语言对象:
SelectFirst[Names["*"], StringLength[#] >= 36&]SelectFirst[z^4 π^2 x^2 y^2, NumericQ]从 PoissonDistribution 的随机样本中找出第一个素数的位置:
SelectFirst[RandomVariate[PoissonDistribution[100], 10 ^ 3], PrimeQ -> "Index"]sample = Table[SelectFirst[RandomVariate[PoissonDistribution[100], 10 ^ 3], PrimeQ -> "Index"], 10 ^ 4];Histogram[sample, Automatic, PDF]FindDistribution[sample]属性和关系 (4)
SelectFirst[{1015177, "Junk", EndOfFile}, PrimeQ,
Print["CriticalError"];$Failed]SelectFirst[{1015177, 2015177, 3015177}, PrimeQ,
Print["CriticalError"];$Failed]SelectFirst 只返回第一个匹配的元素:
SelectFirst[{"1", a, Pi, Sqrt[2], E}, NumericQ]Select 返回所有匹配的元素:
Select[{"1", a, Pi, Sqrt[2], E}, NumericQ]可以通过 Select 和 Replace 的组合实现 SelectFirst:
SelectFirst[{"1", a, Pi, Sqrt[2], E}, NumericQ]
SelectFirst[{"1", a, Pi, Sqrt[2], E}, PrimeQ]Replace[Select[{"1", a, Pi, Sqrt[2], E}, NumericQ], {{} :> Missing["NotFound"], l_ :> First[l]}]
Replace[Select[{"1", a, Pi, Sqrt[2], E}, PrimeQ], {{} :> Missing["NotFound"], l_ :> First[l]}]SelectFirst 与 FirstCase 相似,除非它使用函数而不是模式:
data = <|a -> {3, 7}, b -> {7, 4}, c -> {3, 5}, d -> {6, 4}|>;f = Total[#] < 10&;SelectFirst[data, f]使用 FirstCase 获取相同结果:
FirstCase[data, x_ /; f[x]]文本
Wolfram Research (2014),SelectFirst,Wolfram 语言函数,https://reference.wolfram.com/language/ref/SelectFirst.html (更新于 2025 年).
CMS
Wolfram 语言. 2014. "SelectFirst." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2025. https://reference.wolfram.com/language/ref/SelectFirst.html.
APA
Wolfram 语言. (2014). SelectFirst. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/SelectFirst.html 年
BibTeX
@misc{reference.wolfram_2026_selectfirst, author="Wolfram Research", title="{SelectFirst}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/SelectFirst.html}", note=[Accessed: 21-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_selectfirst, organization={Wolfram Research}, title={SelectFirst}, year={2025}, url={https://reference.wolfram.com/language/ref/SelectFirst.html}, note=[Accessed: 21-June-2026]}