GatherBy
范例
打开所有单元 关闭所有单元基本范例 (2)
应用 (6)
words = RandomChoice[WordData[], {1000}];f = StringTake[#, {1}]&;GatherBy[SortBy[words, f], f]//ShortTuples[{{a, b}, {1, 2}, {x, y}}]GatherBy[%, {First, Last}]用 Framed 显示分组:
Map[Framed, %, {1, 2}]GatherBy[Range[10], Mod[#, 3]&]Transpose@{RandomChoice[{a, b}, 5], RandomInteger[10, 5]}GatherBy[%, First]Flatten@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]];ϵ = $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]]}]属性和关系 (1)
GatherBy[list,f] 等价于 Gather[list,(f[#1]===f[#2])&]:
MatrixForm[m = RandomInteger[1, {10, 10}]]按 Norm 收集行,按每个组的普通规范显示:
Grid[Map[{MatrixForm[#], Norm[First[#]]}&, gb = GatherBy[m, Norm]]]和 Gather 的结果比较:
Gather[m, (Norm[#1] === Norm[#2])&] === gb文本
Wolfram Research (2008),GatherBy,Wolfram 语言函数,https://reference.wolfram.com/language/ref/GatherBy.html.
CMS
Wolfram 语言. 2008. "GatherBy." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/GatherBy.html.
APA
Wolfram 语言. (2008). GatherBy. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/GatherBy.html 年
BibTeX
@misc{reference.wolfram_2026_gatherby, author="Wolfram Research", title="{GatherBy}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/GatherBy.html}", note=[Accessed: 03-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_gatherby, organization={Wolfram Research}, title={GatherBy}, year={2008}, url={https://reference.wolfram.com/language/ref/GatherBy.html}, note=[Accessed: 03-November-2026]}