SelectFirst[data,crit]
crit[ei]がTrueになる data の最初の eiを返す.見付からない場合はMissing["NotFound"]を返す.
SelectFirst[data,critprop]
選択された要素の特性 prop を返す.
SelectFirst[data,crit,default]
crit[ei]がTrueになるdata の eiが存在しない場合に default を返す.
SelectFirst[crit]
式に適用可能なSelectFirstの演算子形を表す.
SelectFirst
SelectFirst[data,crit]
crit[ei]がTrueになる data の最初の eiを返す.見付からない場合はMissing["NotFound"]を返す.
SelectFirst[data,critprop]
選択された要素の特性 prop を返す.
SelectFirst[data,crit,default]
crit[ei]がTrueになるdata の 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[…] 一般的な階層データ - 特性y prop は以下の形式と解釈を持つことができる.
-
"Element" 選択された値 » "Index" 選択された値のインデックスを返す » "BitVectorMask" 選択された値には True,それ以外の値にはFalseを返すブールマスク » {prop1,prop2,…} 複数の形式のリスト » All 要素,インデックス,ビットベクトルマスクを与える連想 » - SelectFirst[data,crit,default]の default は,crit[ei]がTrueになる data が eiがない場合にのみ評価される.
- 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&]SelectFirstをTabularオブジェクトに使う:
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]&][%]SelectFirstを名前付きの列があるTabularオブジェクトに使う:
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[#]&]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]SelectFirstは,SelectとReplaceの組合せで実装することができる:
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 Language. 2014. "SelectFirst." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/SelectFirst.html.
APA
Wolfram Language. (2014). SelectFirst. Wolfram Language & System Documentation Center. Retrieved from 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: 12-November-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: 12-November-2026]}