For coding practice / interview exercises, I'd like to know if there's an optimizaton I can make to the following, where I "clean" a given word to remove punctuation or other characters that are not within "a" to "z".
There are some great answers here to remove punctuation from a string, so my question today is not the best way how to do this, but instead whether there is an optimization I can make to my 3 lines of code below in the
word_count_engine function? Can I do this in 1 or 2 lines or make the code more efficient so it doesn't loop over the list twice (i.e. with 2 list comprehensions)?
def clean(word): returnword = "" for letter in word.lower(): if letter >= 'a' and letter <='z': # not out of bounds returnword += letter return returnword def word_count_engine(document): words = document.split() # if there are extra spaces, split() still filters empty words out FYI words = [clean(word) for word in words] # a word like "$33!" will result in an empty string though words = [word for word in words if word] # so filter out empty strings and get the final list of clean words document = "Practice makes perfect. you'll only get Perfect by practice. just practice! $544 test"