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"))5 odpowiedzi
+ 3
Do you want to replace the duplicated characters if they are consecutively or when the second character occurance is wherever it appears?
+ 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).
+ 2
Thank you😊
0
Am just in sowing stage of python nd I didn't getting why this recursive code isn't working...
0
When the second character occurs