简体   繁体   English

Python “如果 a 和 b 或 a 和 c”的语法,其中 a、b 和 c 是句子中的单词

[英]Python syntax for “if a and b or a and c” where a, b and c are words in a sentence

I have a python script, and I should check if two words are present in a sentence separated by dash我有一个 python 脚本,我应该检查一个用破折号分隔的句子中是否存在两个单词

test_dash_sentence="wordtest-wordtest-word3-word4"

if ("word1" and "word2") or ("word1" and "word3") in test_dash_sentence:
    print("FOUND")
else:
    print("NOT FOUND")

The issue I have is that is return me "FOUND" even if word1 is not present, so it should return me "NOT FOUND"我遇到的问题是即使word1不存在也返回“找到”,所以它应该返回“未找到”

I have tried different models (with simple quotation, without parentheses) and it seems to work if I only look for one word in the sentence, but when I look for two words, it doesn't work.我尝试了不同的模型(简单的引用,没有括号),如果我只在句子中查找一个单词,它似乎可以工作,但是当我查找两个单词时,它不起作用。

the problem is you assume that the and and or syntax is aggrgated to the in later, but this is not true... and you if statment translated to of True in test_dash_sentence: because the statement is casting strings to boolean.. as follows:问题是你假设andor语法被聚合in后面,但这不是真的......你如果of True in test_dash_sentence:因为该语句将字符串转换为 boolean .. 如下:

if ("word1" and "word2") or ("word1" and "word3"):
   print(True):
else:
   print(False)
>>> True

solution:解决方案:

just change:只是改变:

if ("word1" and "word2") or ("word1" and "word3") in test_dash_sentence:

to:至:

if ("word1" in test_dash_sentence and "word2" in test_dash_sentence) or ("word1" in test_dash_sentence and "word3" in test_dash_sentence) :

EDIT: as commented by @JohnnyMopp below you can also make the statement shorter so just:编辑:正如下面@JohnnyMopp 所评论的,您还可以使语句更短,这样就可以了:

if ("word1" in test_dash_sentence) and ("word2" in test_dash_sentence or  "word3" in test_dash_sentence) :

MORE EXPLAINATION BY REQUEST应要求提供更多说明

python does not support the syntax of (element1 and element2 ) in mylist , the in keyword can be only applied to one element at a time so instead you must do element1 in list and element2 in mylist , what happening is python treat (element1 and element2 ) as a separate statement, evaluates it to boolean and then check if the resulting boolean is exist in the mylist . python 不支持(element1 and element2 ) in mylist的语法, in关键字一次只能应用于一个元素,因此您必须element1 in list and element2 in mylist ,发生的情况是 python 处理(element1 and element2 )作为单独的语句,将其评估为 boolean ,然后检查生成的 boolean 是否存在于mylist中。

You must repeat in test_dash_sentence for each word, with the simplification of word1 being in both clauses, you get您必须为每个单词in test_dash_sentence中重复, word1的简化出现在两个子句中,您会得到

if "word1" in test_dash_sentence and ("word2" in test_dash_sentence or "word3" in test_dash_sentence):

You can also use all/any您也可以使用all/any

required = ['word1']         # all of them must be found
any_of = ['word2', 'word3']  # at least one of them must be found

if all(w in test_dash_sentence for w in required) and \
   any(w in test_dash_sentence for w in any_of):
    print("FOUND")
else:
    print("NOT FOUND")

For multiple possible required word having each their optionnal words (one must match), you can use a dict to store, and a 2-level any then对于多个可能需要的单词,每个单词都有它们的可选单词(一个必须匹配),您可以使用dict存储,然后使用2-level any

words = {
    'w1': ['opt1', 'opt2'],
    'w2': ['opt4', 'opt5', 'opt44', 'opt55'],
    'w3': ['opt6', 'opt7'],
}

if any(
        required in test_dash_sentence and any(w in test_dash_sentence for w in any_of)
        for required, any_of in words.items()
):
    print("FOUND")
else:
    print("NOT FOUND")

your boolean condition is not correct.您的 boolean 条件不正确。 Also, "word1" is twice in your condition, so you can combine them:此外, "word1"在您的情况下是两次,因此您可以将它们组合起来:

test_dash_sentence="wordtest-wordtest-word2-word3"

if "word1" in test_dash_sentence and ("word2" in test_dash_sentence or "word3" in test_dash_sentence):
    print("FOUND")
else:
    print("NOT FOUND")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM