4

I have two strings like this.

string1 = "Today I went to the (market) to pick up some (fruit)."
string2 = "Today I went to (school) to learn (algebra) and science."

I want to remove and replace the parenthesis and the text within the parenthesis in both strings based on the following dictionary.

word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'};

I want the new strings to be:

string1 = "Today I went to the library to pick up some books."
string2 = "Today I went to class to learn calculus and science."

In order to do this I need to capture the text within the parenthesis so that I can use that as a key to get the value in the dictionary. Then I need to replace the parenthesis and the text with that value. I'm having trouble with the regex and how to go about doing this.

2
  • Take a look at this, the use of dictionary may do the job:<br> stackoverflow.com/questions/20677660/… Commented Aug 26, 2015 at 16:38
  • Where do the original strings come from? Is it possible to have all the (word)s replaced with {word}? Commented Aug 26, 2015 at 17:25

3 Answers 3

6

You could use str.replace():

string1 = "Today I went to the (market) to pick up some (fruit)."
string2 = "Today I went to (school) to learn (algebra) and science."
word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'}

for word, translation in word_dict.items():  # Use word_dict.iteritems() for Python 2
    string1 = string1.replace('(' + word + ')', translation)
    string2 = string2.replace('(' + word + ')', translation)

You could also use str.format() if you can control the initial strings to use {} instead of ():

string1 = "Today I went to the {market} to pick up some {fruit}."
string2 = "Today I went to {school} to learn {algebra} and science."
word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'}

string1 = string1.format(**word_dict)
string2 = string2.format(**word_dict)

If you can't control the initial output, but would like to use str.format() anyways, you can replace any occurrences of ( and ) with { and }:

string1 = string1.replace('(', '{').replace(')', '}').format(**word_dict)
string2 = string2.replace('(', '{').replace(')', '}').format(**word_dict)

Or to do the same in a much cleaner way, you can use str.translate() along with str.maketrans():

trd = str.maketrans('()', '{}')
string1 = string1.translate(trd).format(**word_dict)
string2 = string2.translate(trd).format(**word_dict)

Keep in mind that this will replace any parenthesis with curly brackets even if they're not surrounding a word you want to replace. You could reverse translate the remaining curly brackets using rev_trd = str.maketrans('{}', '()') after you've formatted the string; but usually at that point you're better off just using the for loop and str.replace() as shown in the first code section. Unless you can change the initial strings to just contain curly brackets, then use that.

Sign up to request clarification or add additional context in comments.

Comments

2

You may use dict.items()

>>> string1 = "Today I went to the (market) to pick up some (fruit)."
>>> string2 = "Today I went to (school) to learn (algebra) and science."
>>> word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'}
>>> for i,j in word_dict.items():
    string1 = string1.replace('(' + i + ')', j)
    string2 = string2.replace('(' + i + ')', j)


>>> string1
'Today I went to the library to pick up some books.'
>>> string2
'Today I went to class to learn calculus and science.'

2 Comments

I feel like this should be a comment to my answer. He asked how to replace stuff in strings and your answer is "You may use dict.items()"? I mean, it's exactly my answer with one small improvement unrelated to the question, and it doesn't work to well for Python 2.
@MarkusMeskanen I think mine would be better than yours in terms of performance.
1

A way you can do this is by splitting the string into words and whitespace, then searching for any item that begins with (. After this, simply replace the item in the list and re-concatenate the list into a string. For example.

word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'};
string1 = "Today I went to the (market) to pick up some (fruit)."
string_list = string1.split()
for item in string_list:
    if item[0] == '(':
        i = string_list.index(item)
        new_string = item[1:-1]
        string_list[i] == word_dict[new_string]
print ' '.join(word[0] for word in string_list)

Best of luck and happy coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.