ReplacePart[expr,inew]
产生一个表达式,用 new 替换 expr 的第 i 个元素.
ReplacePart[expr,{i1new1,i2new2,…}]
用 newn 替换位置 in 处的元素.
ReplacePart[expr,{i,j,…}new]
替换位置 {i,j,…} 处的元素.
ReplacePart[expr,{{i1,j1,…}new1,…}]
用 newn 替换位置 {in,jn,…} 处的元素.
ReplacePart[expr,{{i1,j1,…},…}new]
用 new 替换位置 {in,jn,…} 处的所有元素.
ReplacePart[inew]
表示 ReplacePart 的运算符形式,可用于表达式的计算.
ReplacePart
ReplacePart[expr,inew]
产生一个表达式,用 new 替换 expr 的第 i 个元素.
ReplacePart[expr,{i1new1,i2new2,…}]
用 newn 替换位置 in 处的元素.
ReplacePart[expr,{i,j,…}new]
替换位置 {i,j,…} 处的元素.
ReplacePart[expr,{{i1,j1,…}new1,…}]
用 newn 替换位置 {in,jn,…} 处的元素.
ReplacePart[expr,{{i1,j1,…},…}new]
用 new 替换位置 {in,jn,…} 处的所有元素.
ReplacePart[inew]
表示 ReplacePart 的运算符形式,可用于表达式的计算.
更多信息和选项
- 负数元素表示从尾部开始计数d. »
- 元素的位置指定可以用模式.
- 每个模式测试 expr 中每个子集的每个元素.
- 非明确列表的模式 p (例如 x_) 作为 {p} 处理.
- 模式可以包含类似 __ 的结构,表示可变长度的位置指定.
- ReplacePart[expr,i:>new] 在不先计算的条件下替换元素. 使用类似 patt:>new 的规则,对每个匹配 patt 的元素分别计算 new.
- 缺省选项设置 Heads->Automatic,头部仅在对应位置指定为 0 下进行替换.
- 设置 Heads->True,每次位置指定匹配 0 时替换头部.
- Heads->False 从不替换头部.
- ReplacePart 可以用于 SparseArray 对象.
- ReplacePart[i->new][expr] 等价于 ReplacePart[expr,i->new].
范例
打开所有单元 关闭所有单元基本范例 (6)
ReplacePart[{a, b, c, d, e}, 3 -> xxx]ReplacePart[{a, b, c, d, e}, {2 -> xx, 5 -> yy}]ReplacePart[{{a, b}, {c, d}}, {2, 1} -> xx]ReplacePart[{{a, b}, {c, d}}, {i_, i_} -> xx]ReplacePart[a + b + c ^ n, {{3, 2} -> x + y, 2 -> b ^ 100}]使用 ReplacePart 操作符:
ReplacePart[3 -> xxx][{a, b, c, d, e}]范围 (11)
ReplacePart[{a, b, c, d, e, f, g}, -3 -> xxx]ReplacePart[{a, b, c, d, e, f, g}, {{1}, {3}, {5}} -> xxx]ReplacePart[{a, b, c, d, e, f, g}, (1 | 3 | 5) -> xxx]ReplacePart[{a, b, c, d, e, f, g}, Except[1 | 3 | 5] -> xxx]ReplacePart[{a, b, c, d, e, f, g}, _ ? EvenQ -> xxx]ReplacePart[{{a, b, c}, {d, e}, {f}}, {1, _} -> xx]ReplacePart[{{a, b, c}, {d, e}, {f}}, {_, -1} -> xx]ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {i_, i_} -> x]ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {i_, i_} :> f[i]]ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {___, 2, ___} -> x]ReplacePart[{{a, b, c}, {d, e}, {f}}, i__ :> s[i]]ReplacePart[{<|"x" -> 1, "y" -> 2|>}, {1, 1} -> g]ReplacePart[{<|"x" -> 1, "y" -> 2|>, <|"x" -> 3, "y" -> 4|>}, {_, "x"} -> f]ReplacePart[<|"x" -> 1, "y" -> 2|>, {0} -> f]推广和延伸 (4)
ReplacePart[{{a, b}, {c, d}}, {i_, i_} :> RandomReal[]]ReplacePart 对 SparseArray 对象起作用:
ReplacePart[SparseArray[5 -> a, 10], 7 -> b]Normal[%]ReplacePart 作用于头部:
ReplacePart[f[x, y], 0 -> g]ReplacePart[f[g][x, y], {0, 1} -> hh]List 替换所有头部:
ReplacePart[a x ^ 2 + y ^ 2 + c z ^ 2, {___, 0} -> List]选项 (3)
应用 (8)
ReplacePart[IdentityMatrix[5], {_, 1 | 5} -> x]//MatrixFormArrayPlot[ReplacePart[Array[GCD, {15, 15}], {{6, 6}, {12, 12}} -> Red]]With[{u = RandomInteger[1, 100]}, ArrayPlot[Sum[(-1) ^ i CellularAutomaton[30, ReplacePart[u, 50 -> i], 50], {i, 0, 1}]]]ArrayPlot[NestList[ReplacePart[#, RandomInteger[{1, 30}] -> 1]&, Table[0, {30}], 20], Mesh -> All]FoldList[ReplacePart[#1, #2 -> x]&, {a, b, c, d, e}, {5, 2, 3, 1, 4}]g = Graphics[{Gray, Table[Disk[RandomReal[5, 2]], {5}]}, ImageSize -> Tiny]Position[g, Disk]FoldList[ReplacePart[#1, #2 -> Circle]&, g, %]ArrayPlot[#, Mesh -> True, ImageSize -> 50]& /@ NestList[ReplacePart[#, RandomInteger[{1, 5}, {2}] -> 1]&, ConstantArray[0, {5, 5}], 10]ReplacePart[ConstantArray[0, {5, 5}], {x_, y_} /; CoprimeQ[x, y] :> x + y]属性和关系 (5)
ReplacePart 按给出次序应用规则:
ReplacePart[{a, b, c, d, e}, {3 -> u, _ -> x}]ReplacePart 将相同形式的位置列表作为 Position 的生成结果:
Position[{a, b, x, c, d, x, e}, x]ReplacePart[{a, b, x, c, d, x, e}, {{3}, {6}} -> yy]ReplacePart 采用和 SparseArray 相同的元素规则:
SparseArray[{1 -> x, 5 -> y}, 10]//NormalReplacePart[Array[0&, 10], {1 -> x, 5 -> y}]ReplacePart 重写指定位置上的子表达式:
ReplacePart[f[1, x, 3], {2} -> 0]Replace 重写指定层上与模式匹配的部分:
Replace[f[1, x, 3], Except[_ ? NumericQ] :> 0, {1}]ReplacePart 替换位置和模式匹配的表达式的部分:
ReplacePart[{5, 4, 3, 2, 1} , _ ? PrimeQ -> "p"]ReplaceAll 替换和模式匹配的表达式的部分:
{5, 4, 3, 2, 1} /. _ ? PrimeQ -> "p"可能存在的问题 (4)
ReplacePart 只作用于已经出现的部分:
ReplacePart[{a, b, c, d}, 5 -> x]特别是在 Orderless 函数中,替换后元素的顺序可能会发生变化:
ReplacePart[ReplacePart[a + b + c, 1 -> x], 3 -> y]ReplacePart[h[a, b], {} -> x]ReplacePart[h[a, b], {{}} -> x]ReplacePart[<|"x" -> 1, "y" -> 2|>, {0} -> List]Normal[<|"x" -> 1, "y" -> 2|>]技术笔记
-
▪
- 添加、删除和修改列表元素 ▪
- 表达式的项 ▪
- 通过索引操作列表
历史
1991年引入 (2.0) | 在以下年份被更新:1996 (3.0) ▪ 2003 (5.0) ▪ 2007 (6.0) ▪ 2014 (10.0)
文本
Wolfram Research (1991),ReplacePart,Wolfram 语言函数,https://reference.wolfram.com/language/ref/ReplacePart.html (更新于 2014 年).
CMS
Wolfram 语言. 1991. "ReplacePart." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2014. https://reference.wolfram.com/language/ref/ReplacePart.html.
APA
Wolfram 语言. (1991). ReplacePart. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/ReplacePart.html 年
BibTeX
@misc{reference.wolfram_2026_replacepart, author="Wolfram Research", title="{ReplacePart}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/ReplacePart.html}", note=[Accessed: 03-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_replacepart, organization={Wolfram Research}, title={ReplacePart}, year={2014}, url={https://reference.wolfram.com/language/ref/ReplacePart.html}, note=[Accessed: 03-November-2026]}