![]() |
string manipulation - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: string manipulation (/thread-9440.html) |
string manipulation - yogi123 - Apr-08-2018 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 RE: string manipulation - scidam - Apr-09-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])
RE: string manipulation - yogi123 - Apr-09-2018 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. RE: string manipulation - scidam - Apr-09-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 -> xor Quote:He -> HTSince the translation table (my_dict) is an unordered structure, it is impossible to solve which variant of translation is right... RE: string manipulation - yogi123 - Apr-10-2018 (Apr-09-2018, 10:58 PM)scidam Wrote: Your task is quite sophisticated ... Lets consider your translation tablemy_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 -> xorQuote:He -> HTSince 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 RE: string manipulation - scidam - Apr-12-2018 So, 1) you need to sort my_dict in descending order by key length and translate it to an ordered listof 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!' #resultHope that helps... |