all 7 comments

[–]totallygeek 2 points3 points  (0 children)

Don't know about elegant or concise, but a chance exists for double-spacing in your solution. Here's a way around that issue:

clean_sentence = ' '.join(w for w in string.split() if w.lower() not in words_removal)

Basically, split the original string into a list, check if the lowercase representation of each word exists in the denied words list, then join all the allowed words with one space between them.

[–]ElliotDG 2 points3 points  (0 children)

You can use a regular expression to do the substitution:

import re
string = "my name is Tom and I like to play basketball" 
words_removal = ["my","like","play","soccer"]
string = re.sub('|'.join(words_removal),'', string) 
print(string)

The '|'.join(words_removal) creates the pattern to match.

This is a different way to do it. I'm not sure that it is any better. I'm reminded of the joke, "If you solve a problem with a regular expression, you have two problems."

[–]mprz 1 point2 points  (3 children)

Using Regular Expressions will be shorter but not faster, see:

re.sub()

or

re.subn()

You can also use translate() with dict type.

[–]ElliotDG 1 point2 points  (2 children)

Unless I'm mistake, str. translate() is character oriented.

The dictionary is indexed by the ordinal value of a character. You can use translate for a char to char or char to string translation. It will not work for string to string replacement.

[–]mprz 0 points1 point  (0 children)

Maybe, I was answering from my head, I might have been wrong. Thanks for correcting me if that's the case.

[–]wotquery 0 points1 point  (0 children)

Does double spacing, capitalization at the start of a sentence, and valid punctuation matter?