i think it over night,but couldn’t find a fast way to do it
first came to place is replace()
for x,y in {'aa':'a','ab':'b'}.items():
s = s.replace(x,y)
but it won’t work for this case:’aabcc’
correct is ‘abcc’
but output would be ‘bcc’
i can write some similar code like this (i have already write those code in various language like C#,php,actionscript,etc..)
output = []
def foo1(x):
if(x!='a'):
output.append(x)
else:
functionPointer = readafterA
def readafterA(x):
if(x=='a'):
output.append('a')
elif(x=='b'):
output.append('b')
else:
output.append('a'+x)
functionPointer = foo1
functionPointer = normalExecute
for singlechar in s:
functionPointer(singlechar)
print(''.join(output))
i don’t want write those code any more ,becuz:
1st,those code would messed up when the dictionary go big.
2nd,i believe there has to be some more elegant ways to do this job in Python
edit: i have fixed it in first paragraph,but that was not the point.
You can just not use a dict. Specify the tuples in the order you want to substitute them.
You could also use regular expressions