python program to replace the 2nd occurrence of all characters in a given string?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

python program to replace the 2nd occurrence of all characters in a given string??

def replace(e): for i in range(0,len(e)-1): if i==len(e)-1: return e else: a="" d=[] for j in range(i+1,len(e)): if e[i]==e[j]: c=j d=[e[k:k+c] for k in range(0,len(e),c)] d[1]=d[1].replace(d[1][0],"

quot;) for x in d: a+=x return replace(a) print(replace("restrest"))

19th Mar 2020, 2:20 PM
Krish Sai Krish
Krish Sai Krish - avatar
5 Answers
+ 3
Do you want to replace the duplicated characters if they are consecutively or when the second character occurance is wherever it appears?
19th Mar 2020, 3:17 PM
Lothar
Lothar - avatar
+ 3
This is a generic code that replaces duplicated characters: inp = list("restabcdefgrest") res = [] for i in inp: if i not in res: res.append(i) else: #(1) res.append('
#x27;) # (1) print(''.join(res)) If you want to remove duplicated characters instead of replace them, you can remove the lines marked with #(1).
19th Mar 2020, 3:36 PM
Lothar
Lothar - avatar
+ 2
Thank you😊
19th Mar 2020, 3:39 PM
Krish Sai Krish
Krish Sai Krish - avatar
0
Am just in sowing stage of python nd I didn't getting why this recursive code isn't working...
19th Mar 2020, 2:23 PM
Krish Sai Krish
Krish Sai Krish - avatar
0
When the second character occurs
19th Mar 2020, 3:18 PM
Krish Sai Krish
Krish Sai Krish - avatar