I have a problem with a function I am trying to implement that needs to replace some letters (in a given string), for some other characters, defined on a dictionary.
I have this dictionary:
chars = { 'A': ['4', '@'], 'E': ['3', '€', '&', '£'], 'T': ['7'], 'S': ['$', '§', '5'], 'I': ['1', '!'], 'O': ['0'], 'B': ['8'], 'C': ['<'], 'L': ['1'] }
I want to pass a string that when a character matches any key from the dictionary (case insensitive match), it tries to save all the possible combinations for that letter, using the values on the dictionary.
Here's what I currently have:
def type4 (words, v): chars = { 'A': ['4', '@'], 'E': ['3', '€', '&', '£'], 'T': ['7'], 'S': ['$', '§', '5'], 'I': ['1', '!'], 'O': ['0'], 'B': ['8'], 'C': ['<'], 'L': ['1'] } tmp = [] string = [] for w in words: # each character in a word for key in chars.keys(): # [A, E, T, S, I, O, B, C, L] for c in chars[key]: # [4, @, 3, €, &, £, ...] string = list(w) k = 0 ya = "" for char in string: if (char == key or char == key.lower()): string[k] = c else: string[k] = char k += 1 if (not string == list(w)): for i in string: ya += i print(ya, end="\n\n") tmp.append(ya) return tmp
In the current code, I have only replaced a character on the string. I need to change it so that it matches the output below,
The desired output, using the word sopa, should be like the following:
sopa sop4 sop@ s0pa s0p4 s0p@ $opa $op4 $op@ $0pa $0p4 $0p@ §opa §op4 §op@ §0pa §0p4 §0p@ 5opa 5op4 5op@ 50pa 50p4 50p@
I appreciate any help. Thank y'all in advance.
https://stackoverflow.com/questions/65865618/replace-all-specific-characters-in-string-using-python January 24, 2021 at 07:29AM
没有评论:
发表评论