Count
更多信息和选项
- Count 的第一个参数不需要具有头 List.
- 当用在 Association 上时,Count 仅测试元素的值,而不是它们的键.
- Count 使用标准的层指定:
-
n 第 1 层到第 n 层 Infinity 第 1 层到 Infinity {n} 第 n 层 {n1,n2} 第 n1 层到第 n2 曾 - 在 Count 中,levelspec 的缺省值是 {1}.
- 一个正层数 n 包含由 n 个索引指定的 expr 中的所有部分.
- 一个负层数 -n 包含深度为 n 的 expr 的所有部分.
- 层数 -1 由数字、符号和其它没有子部分的对象组成.
- 层数 0 对应整个表达式.
- 设置选项 Heads->True,Count 查看表达式头部及各部分.
- Count[pattern][expr] 等价于 Count[expr,pattern].
- Parallelize[Count[list,pattern]] 在所有子核上并行计算 Count[list,pattern]. »
范例
打开所有单元 关闭所有单元基本范例 (4)
Count[{a, b, a, a, b, c, b}, b]计算 Association 中 x 的幂次:
Count[<|1 -> 1 + x ^ 2, 2 -> x ^ 4, 3 -> a + (1 + x ^ 2) ^ 2|>, x ^ _]Count[<|1 -> 1 + x ^ 2, 2 -> x ^ 4, 3 -> a + (1 + x ^ 2) ^ 2|>, x ^ _, Infinity]Count[<|1 -> 1 + x ^ 2, 2 -> x ^ 4, 3 -> a + (1 + x ^ 2) ^ 2|>, _Symbol, Infinity]范围 (5)
Count 对模式有效:
Count[{a, 2, a, a, 1, c, b, 3, 3}, _Integer]Count[{a, b, a, a, b, c, b, a, a}, Except[b]]Count[{{a, a, b}, b, {a, b, a}}, b, 2]Count[{{a, a, b}, b, {a, b, a}}, b, {2}]Count[x ^ 3 + 1.5x ^ 2 + Pi x + 7, _ ? NumericQ, -1]Count[5, _ ? NumericQ, -1]Count[5, _ ? NumericQ, {0, -1}]选项 (1)
应用 (3)
属性和关系 (5)
Count[{1, "f", g, "h", "7"}, _ ? StringQ]Length[Cases[{1, "f", g, "h", "7"}, _ ? StringQ]]Count[{1, "f", g, "h", "7"}, _ ? StringQ]Length[Position[{1, "f", g, "h", "7"}, _ ? StringQ]]在层 {0} 的计数等效于 MatchQ 的数值化版本:
MatchQ[5, _Integer]Count[5, _Integer, {0}] == 1对于大多数表达式,LeafCount 等于在层 {-1} 匹配 Blank[] 的计数:
LeafCount[1 + a + b ^ 2]Count[1 + a + b ^ 2, _, {-1}, Heads -> True]Count 将 Rational 和 Complex 视作原子式:
AtomQ /@ {1 / 2, 1 + I}Count[{1 / 2, 1 + I}, _, {-1}, Heads -> True]LeafCount 对 Rational 和 Complex 计数时使用它们的 FullForm:
{1 / 2, 1 + I}//FullFormLeafCount[{1 / 2, 1 + I}]并行计算 Count:
Parallelize[Count[Range[10 ^ 6], _ ? PrimeQ]]技术笔记
-
▪
- 检测和搜索列表元素 ▪
- 寻找与模式匹配的表达式
相关链接
历史
1988年引入 (1.0) | 在以下年份被更新:2014 (10.0)
文本
Wolfram Research (1988),Count,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Count.html (更新于 2014 年).
CMS
Wolfram 语言. 1988. "Count." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2014. https://reference.wolfram.com/language/ref/Count.html.
APA
Wolfram 语言. (1988). Count. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Count.html 年
BibTeX
@misc{reference.wolfram_2026_count, author="Wolfram Research", title="{Count}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/Count.html}", note=[Accessed: 07-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_count, organization={Wolfram Research}, title={Count}, year={2014}, url={https://reference.wolfram.com/language/ref/Count.html}, note=[Accessed: 07-September-2026]}