Python-如何在句子中找到匹配的一个或多个单词

时间:2018-11-18 21:47:04

标签: python string loops if-statement

因此,我正在尝试为自己编写一个小脚本,其中我有一个或多个单词,因此应该在随机句子中找到所有匹配的单词。

等:

Sentence1 = "Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow"

Sentence2 = "Is it beautiful weather"

Sentence3 = "I hope it wont be snowing here soon"

Sentence4 = "How is the weather"

Words = ['I+be', 'it+weather']

输出应该说

Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow

Is it beautiful weather

I hope it wont be snowing here soon

以及它不打印第一个和最后一个的原因是它不包含 I Be it 天气

所以我的问题基本上是如何制作每个 + 或任何其他特殊字符,例如keyword1 + keyword2 + n(可以从1到n个单词),并比较这些单词是否在句子中

所以我尝试编写的代码类似

Sentence = [
    "Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow",
    "Is it beautiful weather", "I hope it wont be snowing here soon",
    "How is the weather"]

Words = ['I', 'it+weather']

for loop_word in Words:
    for loop_setence in Sentence:
        if loop_word in loop_setence:
            print(loop_setence)
            break

但是,到目前为止,它仅会打印出第一句话,因为我现在将单词更改为I。

我想做的是,其中包含一个以上单词的单词应在中间加上一个特殊字符,例如I + be,因此只要句子中有I和Be,它都应打印出该句子-否则不打印任何内容。

Illustration

所以我对你的问题是,我怎样才能继续我的愿望:)?

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

words = ['I+be', 'it+weather']
sentences = ["Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow",
             "Is it beautiful weather", "I hope it wont be snowing here soon", "How is the weather"]

def check_all(sentence, ws):
    return all(w in sentence for w in ws)

for sentence in sentences:
    if any(check_all(sentence, word.split('+')) for word in words):
        print(sentence)

输出

Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow
Is it beautiful weather
I hope it wont be snowing here soon

函数check_all检查句子中是否有一组单词中的所有单词(例如'I+be')。然后,如果句子中有任何一组单词,则应打印该句子。请注意,您必须首先在'+'上进行拆分,以查找组是否匹配。

更新

仅匹配整个单词,我建议您使用regex,例如:

import re

words = ['I+be', 'it+weather']
sentences = ["Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow",
             "Is it beautiful weather", "I hope it wont be snowing here soon", "How is the weather", "With In be"]


def check_all(sentence, ws):
    """Returns True if all the words are present in the sentence"""
    return all(re.search(r'\b{}\b'.format(w), sentence) for w in ws)


for sentence in sentences:
    if any(check_all(sentence, word.split('+')) for word in words):
        print(sentence)

输出

Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow
Is it beautiful weather
I hope it wont be snowing here soon

请注意,第二个示例在输出中不包含"With In be"

进一步

  1. 请参阅anyall上的文档。
  2. Python regular expression match whole word

答案 1 :(得分:1)

使用filteranyallsplit

In [22]: Sentence1 = "Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow"
    ...:
    ...: Sentence2 = "Is it beautiful weather"
    ...:
    ...: Sentence3 = "I hope it wont be snowing here soon"
    ...:
    ...: Sentence4 = "How is the weather"
    ...:
    ...: Words = ['I+be', 'it+weather']
    ...:

In [23]: sentences = [Sentence1, Sentence2, Sentence3, Sentence4]

In [27]: list(filter(lambda s: any(all(w in s.split() for w in word.split('+')) for word in Words), sentences))
    ...:
Out[27]:
['Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow',
 'Is it beautiful weather',
 'I hope it wont be snowing here soon']

如果其中一个关键字在句子之一中,则理解将返回True的{​​{1}}的生成器。如果内部容器的所有元素均为False,则all将返回True。相反,如果内部容器的任何元素为True,则any将返回True

检查True不会返回'be'

Sentence2

请注意,这不会考虑标点符号。即In [43]: Words = ['be'] In [44]: list(filter(lambda s: any(all(w in s.split() for w in word.split('+')) for word in Words), sentences)) Out[44]: ['Hello, I am new here and I hope I will be able to help and get helped from Stackoverflow', 'I hope it wont be snowing here soon']