Below is the Python code to replace characters. Can some explain the lambda part? Initially, X is taking "p" and checking if its a1 or a2. where is the swap happening?
def replaceUsingMapAndLambda(sent, a1, a2):
# We create a lambda that only works if we input a1 or a2 and swaps them.
newSent = map(lambda x: x if(x != a1 and x != a2) else a1 if x == a2 else a2, sent)
print(newSent)
return ''.join(newSent)
print(replaceUsingMapAndLambda("puporials toinp", "p", "t"))
output:
$python main.py
['t', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', ' ', 'p', 'o', 'i', 'n', 't']
tutorials point
Thanks, Reethika