I have a list with n occurrences of a certain element. I have a second list with
Length[lis2] = n
I would like to replace the nth occurrence of the certain element in lis1 with the nth member of lis2.
lis1 = {A,b,c,d,A,e,f,g} Here, A is the special character.
lis2 = {X,Y}
res = {X,b,c,d,Y,e,f,g}
It seems that there should be an easy way of placing the nth member of lis2 at the right place in lis1 using
Position[lis1,A]
Thanks for any ideas!
ReplacePart[lis1, Thread[Flatten[Position[lis1, A]][[;; UpTo@Length@lis2]] -> lis2]]
will get you started. You'll want to harden this with error checking (enough elements to change, etc) if this is not some one-shot need. $\endgroup$ – ciao Sep 20 at 6:07