使用re将句子作为包含特定单词的列表返回

时间:2019-06-19 18:47:05

标签: python

使用re从大字符串中返回包含特定单词的句子。

我不确定如何解决此问题。

def find_sentences_with_keyword(text, keyword):

    splitter = text.split(". ")


the outputs:
string = Just on the report itself, I think people would want to sell the market. However, the fact that it really makes the case for a rate cut, I think is why you're seeing the market hang in there,' said JJ Kinahan, chief market strategist at TD Ameritrade. Market expectations for a Fed rate cut in June rose to 27.5% from 16.7% after the data release, according to the CME Group's FedWatchtool. The market is also pricing in a 79% chance of lower Fed rates by August.

find_sentences_with_keyword("market", string) = ["Just on the report itself, I think people would want to sell the market.", "However, the fact that it really makes the case for a rate cut, I think is why you're seeing the market hang in there,' said JJ Kinahan, chief market strategist at TD Ameritrade.", "Market expectations for a Fed rate cut in June rose to 27.5% from 16.7% after the data release, according to the CME Group's FedWatchtool.", "The market is also pricing in a 79% chance of lower Fed rates by August."]

find_sentences_with_keyword("Market", string) = ["Market expectations for a Fed rate cut in June rose to 27.5% from 16.7% after the data release, according to the CME Group's FedWatchtool."]

1 个答案:

答案 0 :(得分:2)

如何处理(区分大小写):

def find_sentences_with_keyword(text, keyword):
    splitter = text.split(". ")
    return [x for x in splitter if keyword in x]

不区分大小写的情况相同:

def find_sentences_with_keyword(text, keyword):
    splitter = text.split(". ")
    return [x for x in splitter if keyword.lower() in x.lower()]