SubsetReplace

SubsetReplace[list,rules]

replaces sublists in list according to the specified rule or list of rules.

SubsetReplace[list,rules,n]

does only the first n replacements.

SubsetReplace[rules]

represents an operator form of SubsetReplace that can be applied to an expression.

Details

  • In SubsetReplace[list,pattrhs], patt can be an expression with head List that includes pattern constructs such as PatternSequence, Repeated (..), Blank (_) and BlankSequence (__).
  • The pattern patt can also be a pattern that matches a list but does not itself have head List.
  • Sublists matching patt can appear in list in any order.
  • SubsetReplace applies transformation rules in the order in which they appear in rules. Elements of a matching sublist are removed from list and the corresponding rhs is inserted at the position of the element of the sublist that was matched first. Subsequent transformations are applied to elements that have not been transformed yet.
  • SubsetReplace[rules][list] is equivalent to SubsetReplace[list,rules].

Examples

open allclose all

Basic Examples  (3)

Replace a subset of a list:

Replace sublists that match a pattern:

Use the operator form of SubsetReplace:

Scope  (2)

For a variable-length pattern, SubsetReplace replaces the longest match:

Replace sublists with a different list:

Applications  (1)

Delete the first two copies of an element in a list:

The same result can be obtained with DeleteElements:

Wolfram Research (2020), SubsetReplace, Wolfram Language function, https://reference.wolfram.com/language/ref/SubsetReplace.html.

Text

Wolfram Research (2020), SubsetReplace, Wolfram Language function, https://reference.wolfram.com/language/ref/SubsetReplace.html.

CMS

Wolfram Language. 2020. "SubsetReplace." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SubsetReplace.html.

APA

Wolfram Language. (2020). SubsetReplace. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SubsetReplace.html

BibTeX

@misc{reference.wolfram_2024_subsetreplace, author="Wolfram Research", title="{SubsetReplace}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/SubsetReplace.html}", note=[Accessed: 21-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_subsetreplace, organization={Wolfram Research}, title={SubsetReplace}, year={2020}, url={https://reference.wolfram.com/language/ref/SubsetReplace.html}, note=[Accessed: 21-December-2024 ]}