I believe a better solution exists.
ClearAll[replaceLimitedBack, replaceLimited];
replaceLimitedBack[expr_, rulesRaw_, n_Integer, levelSpec_] :=
Block[{rules = Association[rulesRaw], one = 0},
{Replace[expr,
a_?(If[one < n && KeyExistsQ[rules, #], one += 1; True,
False] &) :> rules[a], levelSpec], n - one}]
replaceLimited[expr_, rulesRaw_List, n_Integer : 1, levelSpec_ : Infinity] :=
First[Fold[
replaceLimitedBack[#1[[1]], #2, #1[[2]], levelSpec] &, {expr, n},
rulesRaw]]
In the condition (?), we check how many times we have replaced elements using one + if a replacement exists with KeyExistsQ, then increase the one.
result:
replaceLimited[{"May", "5", "May", "5"}, {"May" -> 1, "5" -> 2}]
(Out: {1, "5", "May", "5"} )
replaceLimited[{"May", "5", "May", "5"}, {"May" -> 1, "5" -> 2}, 2]
(Out: {1, "5", 1, "5"} )
replaceLimited[{"May", "5", "May", "5"}, {"5" -> 2, "May" -> 1}]
(Out: {"May", 2, "May", "5"} )
Notes:
- Rules will be applied one by one, as far as permitted. For example, if the limit is 2 and the first rule occurred 2 times, only the first rule will be applied and the rest of the rules will not be touched (see the second example).
- The third argument (
n) is for how many times you want to replace.
- The fourth argument (
levelSpec) is for Replace LevelSpec.
Replace[{"May", "5", "May", "5", {"May", "5", "May", "5"}, {{"May", "5", "May", "5"}}}, {"May" -> 1, "5" -> 2}, 2]– cvgmt Jul 02 '21 at 06:41ReplacePart[{"May", "5", "May", "5"}, Position[{"May", "5", "May", "5"}, "May"][[1]] -> 1]– cvgmt Jul 02 '21 at 06:46