Given a list, how to get all possible new lists replacing a matched pattern with a new pattern (every time only one replacement)?
For example, I have a list like this {0, 1, 0, 1}
. I want to first find all the {0, 1}
in it. Then each time I replaced one and only one of them with {1, 0}
to get a new list. So in this example the result should be {{1, 0, 0, 1}, {0, 1, 1, 0}}
, each sublist of which is a list replacing one of the matched pattern {0, 1}
with {1, 0}
.
I try {0, 1, 0, 1} /. {x___, PatternSequence[0, 1], y___} -> {x, 1, 0, y}
but this only gives one list {1, 0, 0, 1}
, not whole possible lists.