Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string manipulation
#1
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
Reply
#2
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'
Reply
#3
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
Reply
#4
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...
Reply
#5
(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
Reply
#6
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...
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020