Posts: 3
Threads: 1
Joined: Apr 2018
Apr-08-2018, 09:28 PM
(This post was last modified: Apr-08-2018, 10:51 PM by micseydel.)
I need to replace parts of a string with what it matches in a dictionary. I have restrictions and to change strings only functions i can use are .strip, .split, .append, .join, .format. I cant use replace.
def sub(my_dict, phrase):
for keys, values in my_dict.items():
s = phrase.split(keys)
phrase = values.join(s)
return phrase
my_dict = { 'c':'.', 'p':'f', 'a':'g', 'l':'e', 'e':'l', ' ':'o' }
phrase = "ape pea lap" should return gfloflgoegf
I return gfloflgolgf
Posts: 817
Threads: 1
Joined: Mar 2018
my_dict = { 'c':'.', 'p':'f', 'a':'g', 'l':'e', 'e':'l', ' ':'o' }
phrase = "ape pea lap"
def sub(my_dict, phrase):
return ''.join([my_dict[c] if c in my_dict else c for c in phrase]) Output: >>> sub(my_dict, phrase)
'gfloflgoegf'
Posts: 3
Threads: 1
Joined: Apr 2018
Apr-09-2018, 02:24 AM
(This post was last modified: Apr-09-2018, 03:12 AM by yogi123.)
That would only work for dictionaries that have one letter keys.
Now it wouldn't work for keys with more than one letter:
my_dict = { 'He':'x', 'l':'y', 'd':'n', 'o ':'g', ' fri':'t', 'nd!': 'q', 'e':'T'}
phrase = "Hello friend!"
It should return xyygtTq
But it would just return 'Hello friend!'.
Sorry, I should've mentioned that earlier
(Apr-09-2018, 02:24 AM)yogi123 Wrote: That would only work for dictionaries that have one letter keys.
Now it wouldn't work for keys with more than one letter:
my_dict = { 'He':'x', 'l':'y', 'd':'n', 'o ':'g', ' fri':'t', 'nd!': 'q', 'e':'T'}
phrase = "Hello friend!" It should return xyygtTq
But it would just return 'Hello friend!'.
Sorry, I should've mentioned that earlier
Posts: 817
Threads: 1
Joined: Mar 2018
Your task is quite sophisticated ...
Lets consider your translation table
my_dict = { 'He':'x', 'l':'y', 'd':'n', 'o ':'g', ' fri':'t', 'nd!': 'q', 'e':'T'} It is an unordered set of keys and values (dicts are unordered structures in Python), so
performing translation of the word "Hello" we need to decide how to translate e.g. the "He" term,
Quote:He -> x
or
Quote:He -> HT
Since the translation table (my_dict) is an unordered structure, it is impossible to solve which
variant of translation is right...
Posts: 3
Threads: 1
Joined: Apr 2018
Apr-10-2018, 02:42 PM
(This post was last modified: Apr-10-2018, 02:42 PM by yogi123.)
(Apr-09-2018, 10:58 PM)scidam Wrote: Your task is quite sophisticated ... Lets consider your translation table my_dict = { 'He':'x', 'l':'y', 'd':'n', 'o ':'g', ' fri':'t', 'nd!': 'q', 'e':'T'} It is an unordered set of keys and values (dicts are unordered structures in Python), so performing translation of the word "Hello" we need to decide how to translate e.g. the "He" term, Quote:He -> x
or Quote:He -> HT
Since the translation table (my_dict) is an unordered structure, it is impossible to solve which variant of translation is right...
It would have to make He -> x not HT. Which is why its difficult
Posts: 817
Threads: 1
Joined: Mar 2018
So, 1) you need to sort my_dict in descending order by key length and translate it to an ordered list
of tuples [(key, value), ...],e.g. [('fri', 't'), ('He', 'x') ... ]; This is needed to process longest keys firstly;
2) Next step assumes building a tree-like structure, e.g.:
my_dict = { 'l':'y', 'He':'x'}
sorted_dct = [('He', 'x'), ('l', 'y')]
sample = 'Hello, world!'
['Hello, world!', ''] #first iteration, splitting by ''
[[['', 'llo, world!'], 'x'], '']#second iteration, splitting by 'He'
[[[[''], 'y'], [['', '', 'o, wor', 'd!'],'y'], 'x']] #third iteration, splitting by 'l'
# now we need to recompose the result:
[['', 'yyo, woryd!'], 'x']] # joining inner lists with 'y', e.g. 'y'.join(['']); 'y'.join(['', '', 'o, wor', 'd!'])
[['', 'yyo, woryd!'], 'x']] # final joining 'x'.join(['', 'yyo,woryd!']) leads to
'xyyo, woryd!' #result Hope that helps...
|