Remove suffixes from words in a string that are only less than 8 characters long

Question:

def removeSuffixs(sentence):
    line = sentence.split()

    new_line = []
    string_index_list = []
    string_length_list = []
    suffix_list = ['ed', 'ly', 'ing'] 

    for i in line:
        string_index = line.index(i)
        string_index_list.append(string_index)

        string_length = len(line[string_index])
        string_length_list.append(string_length)


    for x in line:
        for suff in suffix_list:
            for s in string_length_list:
                if x.endswith(suff) and s < 8:
                    x = x.removesuffix(suff)
                else:
                    pass
        new_line.append(x)
        new_line2 = ' '.join(new_line)

    return new_line2

print(removeSuffixs('a boy is jumping quickly tremendously'))

The output is:

a boy is jump quick tremendous

How do I add a conditional that will only remove the suffix from strings that are less than 8 characters long so that my output will be a boy is jump quick tremendously?

Asked By: Ken Lo

||

Answers:

you need to split the sentence into words, then get the length of the word you are changing, like this:

def removeSuffixs(sentence):
    words = sentence.split(' ')
    suffixList = ["ed", "ly", "ing"] #add more as necessary
    output_list = []
    for word in words:
        final_word = word
        for suffix in suffixList:
            if suffix in word and len(word)<8:
                final_word = word.replace(suffix, '')
        output_list.append(final_word)
    return ' '.join(output_list)

first, I’m splitting the sentence into a list of words. Then looping through those words to check every single one for their length and whether they contain the suffix.
At the end we’re returning the ready sentence using .join() function of string.

I’ve also changed item to suffix so the code is easier to read 🙂

Answered By: Maciej Miecznik

A generator expression and regular expressions make this quite straightforward. For each word in the sentence we either return the word if it’s 8 characters long or longer, or we substitute ly or ing for nothing using re.sub. Then we use str.join to bring it back into a sentence.

>>> import re
>>> s = 'a boy is jumping quickly tremendously'
>>> ' '.join(
...   w if len(w) >= 8 else 
...   re.sub(r'(ed|ly|ing)$', '', w)
...   for w in s.split()
... )
'a boy is jump quick tremendously'

If you don’t want to hard-code in the suffixes, you can have them as a list and compile a regular expression based on joining them with |.

>>> import re
>>> suffixes = ['ed', 'ly', 'ing']
>>> regex = re.compile(f"({'|'.join(suffixes)})$")
>>> s = 'a boy is jumping quickly tremendously'
>>> ' '.join(
...   w if len(w) >= 8 else 
...   regex.sub('', w)
...   for w in s.split()
... )
'a boy is jump quick tremendously'
Answered By: Chris
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.