首页 > 解决方案 > 如何从字符串末尾开始搜索后缀

问题描述

我正在尝试输入'民主被高估'并返回'democr _acy is underrat_ed'

sentence= input()
suffixes = ["acy","tion", "ate",
            "er", "fy", "ize", "able", "ible", "al",
            "esque", "ful", "ic", "ous", "ish", "ive",
            "less", "ed"]
for pattern in suffixes :
    if pattern in sentence:
        out = ''
        par = sentence.partition(pattern)

        while par[1]:
                out += ' _'.join([par[0], par[1]])
                remainder = par[2]
                par = par[2].partition(pattern)
        sentence = ''.join([out, remainder])
print(''.join([out, remainder]))

如您所见,我的输出是“democr _acy is ov _err _at _ed”。我知道我必须在句尾搜索一个后缀并拆分成有意义的后缀。为此,我认为 sentence.endswith 可能有效,但实际上我不确定我该怎么做:(

标签: python-3.xlistsuffixends-with

解决方案


这是另一种不使用导入的方法:

suffixes = ["acy", "tion", "ate", "er", "fy", "ize", "able", "ible", "al", "esque", "ful", "ic", "ous", "ish", "ive", "less", "ed"]

def split_alpha(sentence):
    words = []
    
    # Create an alphabet with words
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    alphabet += alphabet.lower()
    
    # Store the current word being iterated
    current_word = None
    # Store if the current word is a alphabetical word
    is_alpha = False
    
    for char in sentence:
        is_current_alpha = char in alphabet
        
        # If a word is not defined yet
        if current_word is None:
            # Create a new word with the current char
            current_word = char
            is_alpha = is_current_alpha
    
        else:
            # If the current word has the same 
            # 'alphabeticity' of the current char 
            if is_current_alpha == is_alpha:
                current_word += char
            else:
                # Apprend the previous word to `words`
                words.append(current_word)
                
                # Create a new word with the current char
                current_word = char
                is_alpha = is_current_alpha
                
    if current_word is not None:
        words.append(current_word)
                
    return words

def suffixize(sentence):
    # Split the sentence into words
    words = split_alpha(sentence)
    
    # Split the original sentence in spaces and iterate over each word
    for word in words:
        # If this word ends with some suffix, return this suffix, else return None
        suffix = next((suffix for suffix in suffixes if word.endswith(suffix)), None)
  
        # If this word does not end with any suffix
        if suffix is None:
            # Leave it as it is
            words.append(word)
        else:
            # Remove the suffix from the word, append a _ and add the suffix
            words.append(f'{word[:-len(suffix)]}_{suffix}')

    # Join the words using a space
    return ' '.join(words)
    
assert split_alpha("abc") == ["abc"]
assert split_alpha("     ") == ["     "]
assert split_alpha("a1b2c3") == ["a", "1", "b", "2", "c", "3"]
assert split_alpha("hey there") == ["hey", " ", "there"]
assert split_alpha("democracy,   is overrated!") == ["democracy", ",   ", "is", " ", "overrated", "!"]

assert suffixize("Democracy is overrated") == 'Democr_acy is overrat_ed'
assert suffixize("democracy,   is overrated!") == 'democr_acy,   is overrat_ed!'

推荐阅读