Ced
Ced

Reputation: 1549

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

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"

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.

Upvotes: 1

Views: 567

Answers (3)

azro
azro

Reputation: 54168

You must repeat in test_dash_sentence for each word, with the simplification of word1 being in both clauses, you get

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

You can also use 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

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")

Upvotes: 1

adir abargil
adir abargil

Reputation: 5745

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:

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:

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.

Upvotes: 3

ljuk
ljuk

Reputation: 784

your boolean condition is not correct. Also, "word1" is twice in your condition, so you can combine them:

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")

Upvotes: 0

Related Questions