从python语句中查找并删除一个单词(在单词匹配之间)

2024-04-20 05:38:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面这样的句子

mainsentence="My words aren't available give didn't give apple and did happening me"

stopwords=['are','did','word', 'able','give','happen']

如果任何单词与中间的单词匹配,则要删除(例如:“word”应与“words”匹配并删除它,“did”应与“did”匹配并删除它,“able”应删除“available”,因为“able”单词位于“available”中

finalsentence="My apple and me"

尝试以下代码,但

querywords = mainsentence.split()
resultwords  = [word for word in querywords if word.lower() not in stopwords]
result = ' '.join(resultwords)
print(result)

但它只适用于精确匹配。你知道吗

请帮帮我。你知道吗


Tags: andapplemyable单词wordavailableme
3条回答

对于这类问题,可以使用正则表达式的强大功能。你知道吗

import re

你可以得到这样的匹配词:

words = re.findall(r'[a-z]*did[a-z]*', mainsentence)

您也可以替换它们:

re.sub(r'[a-z]*able[a-z]* ', '', mainsentence)

所以最后的答案是:

mainsentence="My words aren't available give didn't give apple and did happening me"

stopwords=['are','did','word', 'able','give','happen']

for word in stopwords:
    mainsentence = re.sub(fr'[a-z\']*{word}[a-z\']* ', '', mainsentence)
# My apple and me

您可以执行以下操作:

>>> ' '.join([word for word in mainsentence.split() if not any([stopword in word for stopword in stopwords])])
'My apple and me'

编辑:这不需要双向检查,只需查看word是否包含stopword
EDIT2:更新了结果和更新的问题参数

不区分大小写的版本:

' '.join([word for word in mainsentence.split() if not any([stopword.lower() in word.lower() for stopword in stopwords])])

下面的代码将满足您在问题中提出的要求,但它不太可能是您想要的结果。 代码的一般基础结构应该是正确的,但是您可能希望更改部分匹配的条件(stopword in testword):

def filter_out_stopwords(text, stopwords):
    result = []
    for word in text.split():
        testword = word.lower()
        flag = True
        for stopword in stopwords:
            if stopword in testword:
                flag = False
                break
        if flag:
            result.append(word)
    return result


' '.join(filter_out_stopwords("My words aren't available give didn't give apple and did happening me", ['are', 'did', 'word', 'able', 'give', 'happen']))
# "My apple and me"

或者,使用列表理解和all()any()可以等价地使用):

def filter_out_stopwords(text, stopwords):                                                                                                   
    return [
        word for word in text.split()
        if all(stopword not in word.lower() for stopword in stopwords)]


' '.join(filter_out_stopwords("My words aren't available give didn't give apple and did happening me", ['are', 'did', 'word', 'able', 'give', 'happen']))
# "My apple and me"

相关问题 更多 >