GatherBy
詳細
- GatherBy[{e1,e2,e3,…},f]は各 i について f[ei]を評価し,eiをどのように集めるかを決める.
- GatherBy[list,…]は,list 中の要素の順番に従った結果を返す.
- GatherBy[list,…]で生成される各サブリストにおける要素の出現順は list におけるそれと等しい.サブリスト全体が list におけるのと同じ順で最初の要素が現れるように並べられる.
- GatherBy[list,Identity]はGather[list]に等しい. »
- GatherByは事実上同値分割を行う.
- GatherBy[list,{f1,f2}]はMap[GatherBy[#,f2]&,GatherBy[list,f1]]に等しい. »
- GatherBy[f][list]はGatherBy[list,f]に等しい. »
例題
すべて開く すべて閉じる例 (2)
スコープ (4)
GatherBy[Range[10], Mod[#, 3]&]第一成分によってグループ化し,そのグループ内では最後の成分によってグループ化する:
Tuples[{{a, b}, {1, 2}, {x, y}}]GatherBy[%, {First, Last}]Framedを使ってグループ化を表示する:
Map[Framed, %, {1, 2}]GatherBy演算子を定義する:
op = GatherBy[Last];整数データを収集するために演算子を用い,各グループを列のそれぞれ別個の行に配置する:
ints = RandomInteger[4, {10, 3}];
op[ints]//Columnwords = RandomChoice[RandomWord[5], {10, 2}];
op[words]//Columndata = Table[AssociationThread[CharacterRange["a", "c"] -> RandomInteger[5, 3]], {10}]Framed /@ GatherBy[data, #a&]GatherByの演算子形式を用いて,キー"a"と"b"の値によって収集する:
Map[Framed, GatherBy[{#a&, #b&}][data], {1, 2}]アプリケーション (4)
words = RandomChoice[WordData[], {1000}];f = StringTake[#, {1}]&;GatherBy[SortBy[words, f], f]//ShortFlatten@Table[DateString[{y, m}, {"Month", "/", "YearShort"}], {y, RandomChoice[Range[2000, 2010], 5]}, {m, RandomChoice[Range[12], 3]}]GatherBy[%, DateList[{#, {"Month", "/", "YearShort"}}][[1]]&]data = Transpose[{RandomReal[5, 10], RandomChoice[{a, b, c}, 10], RandomReal[10, 10]}]gathered = GatherBy[data, #[[2]]&]Map[{#[[1, 2]], Length[#[[All, -1]]]}&, gathered]Map[{#[[1, 2]], Mean[#[[All, -1]]]}&, gathered]最後の tol ビットの丸めるに基づいて,機械数を同値類ごとにグループ化する:
tol = 7;
f[x_] := Module[{re = Round[RealExponent[x, 2]], e}, e = (53 - tol - re);
m = 2 ^ e;
N[Round[x m] / m]];1に近い相異なる機械数の順序付けられていないサンプルを生成する:
ϵ = $MachineEpsilon;
r = RandomSample[Table[1 + k ϵ, {k, 100}]]g = GatherBy[r, f]Round[(g - 1) / ϵ]Table[{InputForm[r = f[x]], (r - 1) / ϵ}, {x, g[[All, 1]]}]特性と関係 (4)
GatherBy[list,Identity]はGather[list]に等しい:
list = RandomInteger[10, 10];
GatherBy[list, Identity] === Gather[list]GatherBy[list,f]はGather[list,(f[#1]===f[#2])&]に等しい:
m = RandomInteger[1, {10, 5}];
GatherBy[m, Norm] == Gather[m, (Norm[#1] === Norm[#2])&]Grid[Map[{MatrixForm[#], Norm[First[#]]}&, GatherBy[m, Norm] ]]GatherBy[list,{f1,f2}]はMap[GatherBy[#,f2]&,GatherBy[list,f1]]に等しい:
list = Range[-4, 4];
GatherBy[list, {Positive, EvenQ}] === Map[GatherBy[#, EvenQ]&, GatherBy[list, Positive]]GatherBy[f][list]はGatherBy[list,f]に等しい:
list = Range[-4, 4];
GatherBy[EvenQ][list] === GatherBy[list, EvenQ]関連するガイド
テキスト
Wolfram Research (2008), GatherBy, Wolfram言語関数, https://reference.wolfram.com/language/ref/GatherBy.html (2026年に更新).
CMS
Wolfram Language. 2008. "GatherBy." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2026. https://reference.wolfram.com/language/ref/GatherBy.html.
APA
Wolfram Language. (2008). GatherBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GatherBy.html
BibTeX
@misc{reference.wolfram_2026_gatherby, author="Wolfram Research", title="{GatherBy}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/GatherBy.html}", note=[Accessed: 04-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_gatherby, organization={Wolfram Research}, title={GatherBy}, year={2026}, url={https://reference.wolfram.com/language/ref/GatherBy.html}, note=[Accessed: 04-November-2026]}