Consider the following:
list={1,2,2,2,3};
I would like to replace all 2 with the string "Test". Replace[list,2->"Test"] does not do it.
Result must be: {1,"Test","Test","Test",3}.
Consider the following:
list={1,2,2,2,3};
I would like to replace all 2 with the string "Test". Replace[list,2->"Test"] does not do it.
Result must be: {1,"Test","Test","Test",3}.
Replace by default works on the whole expression. You can add a level specification
list={1,2,2,2,3};
Replace[list,2->"Test", {1}]
Gives
{1, "Test", "Test", "Test", 3}
You can use:
list /. {2 -> "Test"}
(* Out[]:= {1, "Test", "Test", "Test", 3} *)
For example:
list = {1, 2, 2, 2, 3};
list /. {2 -> "test"}
If[# == 2, "test", #] & /@ list
Replace[list, 2 -> "test", 1]
ReplacePart[list, Position[list, 2] -> "test"]
a /. bbecomesReplaceAll[a,b]which by default, replaces all instances found. – rcollyer Apr 19 '12 at 17:00list/.{1->"A",2->"X"}. I wounder whetherReplaceis able of that too? – John Apr 19 '12 at 17:01Replace[list, {1 -> "B", 2 -> "Test"}, {1}]– Eli Lansey Apr 19 '12 at 17:02a /. {a -> b, b -> c}returnsb. But, you can useReplaceRepeated(//.), instead.a //. {a -> b, b -> c}then returnsc. – rcollyer Apr 19 '12 at 17:05