Replace
更多信息和选项
- 这些规则必须具有 lhs->rhs 或 lhs:>rhs 的形式.
- 可以给出一个规则列表. 按次序应用规则. 返回第一个应用的规则所得到的结果. 如果没有应用到任何一个规则,则返回原表达式 expr.
- 如果规则列表用嵌套列表的形式给出,Replace 将有效地映射到内层列表上. 因此Replace[expr,{{r11,r12},{r21,…},…}] 等价于{Replace[expr,{r11,r12}],Replace[expr,{r21,…}],…}.
- 用 :> 定义的延时规则可以包含 /; 条件.
- Replace 用标准的层次指定:
-
n 第 1 层到第 n 层 Infinity 第 1 层到 Infinity All 所有层,包括第 0 层 {n} 仅应用第 n 层 {n1,n2} 第 n1 层到第 n2 层 - 在 Replace 中 levelspec 缺省值是 {0},对应于整个表达式.
- 正层的 n 由指标 n 指定的 expr 所有部分组成.
- 负层的-n 由带有深度 n 的 expr 所有部分组成.
- 层 -1 由不带子集的数值、符号和其它对象组成.
- 如果 levelspec 包括多个层,先匹配给定的子表达式中更深层的表达式.
- 选项设置 Heads->True 下,Replace 也作用于表达式的头部和其它部分.
- 对 levelspec 指定的部分应用替换,甚至在它们有 Hold 或相关包装下.
- Replace[rules][expr] 等价于 Replace[expr,rules].
范例
打开所有单元 关闭所有单元基本范例 (4)
缺省情况下,Replace 将规则仅应用到整个表达式上:
Replace[x ^ 2, x ^ 2 -> a + b]Replace[1 + x ^ 2, x ^ 2 -> a + b]Replace[x, {{x -> a}, {x -> b}}]使用操作符形式的 Replace:
Replace[x ^ 2 -> a][x ^ 2]Replace[f[2], f[x_] :> x]Replace[f[2], {f[x_] :> x, f[2] :> "two"}]Replace[f[2], {{f[x_] :> x}, { f[2] :> "two"}}]范围 (15)
Replace[x, {y -> 2, z -> 3}]Replace[1 + x ^ 2, x ^ 2 -> a + b, {1}]替换列表中测试方程的结果为 True 的元素:
Replace[{1, 3, 2, x, 6, Pi}, _ ? PrimeQ -> "prime", {1}]Replace[{1, 3, 2, x, 6, Pi}, t_ /; Mod[t, 3] == 0 -> "3k", {1}]Replace[{{1., 2.}, {Missing["NotAvailable"], -.5}}, x_Missing :> 0., {2}]Replace[{1, Pi, {2 z, Cos[x]}}, x_ -> f[x], {-1}]Replace[f[f[f[f[x]]]], f[x_] :> g[x], 3]Replace[f[f[f[f[x]]]], f[x_] :> g[x], {0, 2}]Replace[f[f[f[f[x]]]], f[x_] :> g[x], All]Replace 还可用于 RuleDelayed:
Replace[{x, x, x}, x :> RandomReal[], {1}]Replace[Hold[x + x], x -> 7, {-1}]Replace[Hold[7 + 7], x_ :> x + x, {-1}]Replace[Hold[7 + 7], x_ -> x + x, {-1}]Dispatch 可以代替规则列表:
Replace[{1, 2}, Dispatch[{1 -> a, 3 -> b}], 1]Association 可以代替规则列表:
Replace[{a, b}, Association[{a -> 1, c -> 2}], 1]对 Association 中的所有值应用函数:
Replace[<|a -> 1, b -> 2|>, x_ -> f[x], {1}]反转 Association 中的键和值:
Replace[<|a -> 1|>, <|key_ -> val_|> :> <|val -> key|>]把 Association 的规则提取到列表中:
Replace[<|a -> 1, b -> 2|>, <|x___|> -> {x}]Replace[<| key -> <|a -> 1, b -> 2|>|>, <|k_ -> v_, y___|> -> {k, v, y}, {1}]选项 (2)
Heads (2)
Replace[f[1, 2, f[3]], f -> g, All]Replace[f[1, 2, f[3]], f -> g, All, Heads -> True]当 Heads->True 时,标头被视为与参数处于同一深度:
Replace[f[1, 2, f[3]], f -> g, {1}, Heads -> True]Replace[f[1, 2, f[3]], f -> g, {2}, Heads -> True]Level[f[1, 2, f[3]], {1}, Heads -> True]Level[f[1, 2, f[3]], {2}, Heads -> True]因此,Heads->True 对于缺省的 {0} 层没有影响:
Replace[f[1, 2, f[3]], Heads -> True]应用 (1)
f[0] := 1
f[n_ ? Positive] := n f[n - 1]f[5]Hold 输入,用 f 的 DownValues 来替换,对运算的单个步骤进行检查:
Replace[Hold[f[5]], DownValues[f], -1]Replace[Hold[5 f[5 - 1]], DownValues[f], -1]属性和关系 (12)
Replace[x, {}]Replace[x, {{x -> 1}, {y -> 2}}]层规定 All 等价于 {0,-1} 和 {0,∞}:
Replace[f[f[f[f[x]]]], f[x_] :> g[x], All]Replace[f[f[f[f[x]]]], f[x_] :> g[x], {0, -1}]Replace[f[f[f[f[x]]]], f[x_] :> g[x], {0, Infinity}]如果规则与多个层相匹配,Replace 将会对所有匹配子表达式进行转换:
Replace[f[f[1], g[f[3]]], f[x__] :> {x}, {0, 2}]Replace[f[f[1], g[f[3]]], f[x__] :> Echo@{x}, {0, 2}]当给定多层时,Replace 使用深度优先搜索扫描表达式:
Replace[{{1, 2}, 3}, x_ /; IntegerQ[Echo[x]] -> "int", All]缺省情况下,Replace 替换整个表达式:
Replace[f[1, 2, z], _Integer :> "int"]Replace[f[1, 2, z], _Integer :> "int", {1}]ReplaceAll 替换所有匹配的部分:
f[1, 2, z] /. _Integer :> "int"带有参数 All 的 Replace 将尝试只对每个子表达式进行一次替换:
rules = {f[x_] -> f[x, 5], f[x__] -> {x, 1}};Replace[{f[a], f[f[a, b]]}, rules, All]ReplaceAll 对所能替换的最大的子表达式进行替换,然后停止操作:
{f[a], f[f[a, b]]} /. rulesReplaceRepeated 重复应用 ReplaceAll 直到表达式不再变化为止:
{f[a], f[f[a, b]]} //. rules% == ({f[a], f[f[a, b]]} /. rules /. rules /. rules)Replace 在特定层进行模式匹配:
Replace[f[1, x, 3], Except[_ ? NumericQ] :> 0, {1}]ReplacePart 重写指定位置上的子表达式:
ReplacePart[f[1, x, 3], {2} -> 0]ReplaceList 给出所有可能的匹配组成的列表:
ReplaceList[a + b + c, x_ + y_ :> {x, y}]Replace 给出第一个标准匹配:
Replace[a + b + c, x_ + y_ :> {x, y}]Replace 可以对整个关联进行转换:
Replace[<| a -> <|b -> 1, c -> 2|>|>, y_ -> foo, {0}]Replace[<| a -> <|b -> 1, c -> 2|>|>, y_ -> foo, {1}]Replace[<| a -> <|b -> 1, c -> 2|>|>, y_ -> foo, {2}]Replace 匹配给定层级的个别表达式:
Replace[{a, b, c, a, b, c, a}, {b -> 1, c -> 2}, 1]SequenceReplace 匹配第一层的表达式序列:
SequenceReplace[{a, b, c, a, b, c, a}, {b, c} -> 12]当且仅当 MatchQ[expr,lhs] 得到 True 结果时,Replace[expr,lhsrhs] 返回转换后的结果:
{Replace[{1}, {x_Integer} :> 2x], MatchQ[{1}, {x_Integer}]}{Replace[{1}, x_Integer :> 2x], MatchQ[{1}, x_Integer]}可能存在的问题 (2)
rules = Table[x[i] -> RandomInteger[{1, i}], {i, 10000}];Timing[Replace[Table[x[i], {i, 10000}], rules, {1}];]使用 Dispatch 会大幅提高速度:
disp = Dispatch[rules];Timing[Replace[Table[x[i], {i, 10000}], disp, {1}];]可以用关联来指定替换规则,但是把键视为 Verbatim 值:
assoc = <|1 -> True, _ -> False|>;Replace[{1, 2, _}, assoc, {1}]list = Normal[assoc]Replace[{1, 2, _}, list, {1}]Replace[{1, 2, _}, {1 -> True, Verbatim[_] -> False}, {1}]技术笔记
-
▪
- 运用变换规则
相关链接
历史
1988年引入 (1.0) | 在以下年份被更新:1999 (4.0) ▪ 2000 (4.1) ▪ 2002 (4.2) ▪ 2014 (10.0)
文本
Wolfram Research (1988),Replace,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Replace.html (更新于 2014 年).
CMS
Wolfram 语言. 1988. "Replace." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2014. https://reference.wolfram.com/language/ref/Replace.html.
APA
Wolfram 语言. (1988). Replace. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Replace.html 年
BibTeX
@misc{reference.wolfram_2026_replace, author="Wolfram Research", title="{Replace}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/Replace.html}", note=[Accessed: 27-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_replace, organization={Wolfram Research}, title={Replace}, year={2014}, url={https://reference.wolfram.com/language/ref/Replace.html}, note=[Accessed: 27-June-2026]}