Depth[expr]
给出指定 expr 的任意部分所需最大指标,并加 1.
Depth
Depth[expr]
给出指定 expr 的任意部分所需最大指标,并加 1.
范例
打开所有单元 关闭所有单元基本范例 (3)
Depth[a]Depth[{a}]Depth[{{a}}]Depth[{{{a}}}]Depth[{{{a}, b}}]Depth 适用于任意表达式:
Depth[1 + x ^ 2]用e TreeForm 将深度可视化为表达式的层级数:
TreeForm[1 + x ^ 2]范围 (10)
一般表达式的深度 (5)
Depth 对任意表达式起作用,不仅仅是列表:
Depth[f[f[f[x]]]]Depth 不区别头部:
Depth[f[g[h[x]]]]Depth[symbol]Depth["string"]Depth[12345]Depth[3 + I]Depth[(1/2)]NumberQ 结果为假的数值表达式的深度更大:
Depth[Sqrt[2]]默认情况下,Depth 不计入标头中的深度:
Depth[h[{{{a}}}][x, y]]Depth[h[{{{a}}}][x, y], Heads -> True]特殊表达式的深度 (5)
Depth[<|1 -> a|>]Depth[<|f[g[x]] -> a|>]Depth[{a}]Depth[<|1 -> x|>]Depth[<|1 -> x + y|>]Depth[<|1 -> <|2 -> 3|>|>]Depth 将 SparseArray 和结构化数组对象视为一般列表:
Depth[SparseArray[Automatic, {1, 1, 1, 1}, 0, {1, {{0, 1}, {{1, 1, 1}}}, {x}}]]Depth[SymmetrizedArray[StructuredArray`StructuredData[{4, 4}, {{{1, 2} -> a, {2, 3} -> b},
Symmetric[{1, 2}]}]]]Depth[Graph[{1 -> 2, 3 -> 4}]]Depth[MeshRegion[{{0}, {1}, {2}, {3}}, {Line[{1, 2}], Line[{3, 4}]}]]选项 (1)
应用 (2)
Table[Depth[Integrate[1 / (x ^ n - 1), x]], {n, 10}]求出组合表达式的深度 [更多信息]:
combs = NestList[# /. s[x_][y_][z_] -> x[z][y[z]]&, s[s][s][s[s]][s][s], 4]Depth /@ combsDepth[#, Heads -> True]& /@ combsmoreCombs = NestList[# /. s[x_][y_][z_] -> x[z][y[z]]&, s[s][s][s[s]][s][s], 30];Depth /@ moreCombsDepth[#, Heads -> True]& /@ moreCombs属性和关系 (5)
Depth 一般给出最大指标的长度,并加1:
Position[(1 + x)(2 + y ^ 3), _]Depth[(1 + x)(2 + y ^ 3)]% == Max[Length /@ %%] + 1Depth 考虑表达式的最深的部分:
{Depth[{{a}, {b}}], Depth[{{a}, {f[b]}}]}ArrayDepth 只考虑表达式完全为矩形的层级:
{ArrayDepth[{{a}, {b}}], ArrayDepth[{{a}, {f[b]}}]}对于完全为矩形的表达式,Depth 给出的结果比 ArrayDepth 的结果大 1:
{Depth[{a, b}], ArrayDepth[{a, b}]}{Depth[{{a}, {b}}], ArrayDepth[{{a}, {b}}]}Depth[expr] 是可能的最小的正的层级 k,Level[expr,{k}] 返回空列表:
expr = a + f[x, y ^ n];Table[Level[expr, {k}], {k, 0, Depth[expr]}]如果存在长度为 0 的函数或复合头部,则 Depth 和 Level 都必须使用 HeadsTrue:
expr = a + f[];Table[Level[expr, {k}], {k, 0, Depth[expr]}]Table[Level[expr, {k}, Heads -> True], {k, 0, Depth[expr, Heads -> True]}]NestList 中的连续元素有更大的深度:
NestList[f, x, 5]Depth /@ %可能存在的问题 (3)
Depth 返回的深度比 ArrayDepth 返回的深度大 1:
{Depth[1], ArrayDepth[1]}{Depth[{1, 2, 3}], ArrayDepth[{1, 2, 3}]}Depth 将关联视为一个层级:
Depth[<|a -> x, b -> y|>]Depth[{a -> x, b -> y}]当存在复合头部时,Depth 和 Position 之间的关系可能会崩溃:
Depth[f[][]] == 1 + Max[Length /@ Position[f[][], _]]在 Depth 中设置 HeadsTrue 以恢复其关系:
Depth[f[][], Heads -> True] == 1 + Max[Length /@ Position[f[][], _]]历史
1988年引入 (1.0) | 在以下年份被更新:2003 (5.0) ▪ 2010 (8.0) ▪ 2018 (11.3)
文本
Wolfram Research (1988),Depth,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Depth.html (更新于 2018 年).
CMS
Wolfram 语言. 1988. "Depth." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2018. https://reference.wolfram.com/language/ref/Depth.html.
APA
Wolfram 语言. (1988). Depth. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Depth.html 年
BibTeX
@misc{reference.wolfram_2026_depth, author="Wolfram Research", title="{Depth}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/Depth.html}", note=[Accessed: 10-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_depth, organization={Wolfram Research}, title={Depth}, year={2018}, url={https://reference.wolfram.com/language/ref/Depth.html}, note=[Accessed: 10-September-2026]}