【问题标题】:Python syntax for “if a and b or a and c” where a, b and c are words in a sentence“如果 a 和 b 或 a 和 c”的 Python 语法,其中 a、b 和 c 是句子中的单词
【发布时间】:2020-12-28 17:37:35
【问题描述】:

我有一个 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")

我遇到的问题是即使word1 不存在也返回“找到”,所以它应该返回“未找到”

我尝试了不同的模型(用简单的引号,不带括号),如果我只在句子中查找一个单词似乎可以工作,但是当我查找两个单词时,它就不起作用了。

【问题讨论】:

标签: python python-3.x if-statement operators


【解决方案1】:

问题是你假设andor 语法后来被聚合到in,但这不是真的...... 如果语句转换为of True in test_dash_sentence:,因为语句将字符串转换为布尔值.. 如下:

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

解决方案:

只是改变:

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

到:

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

编辑:正如下面@JohnnyMopp 所评论的那样,您也可以使语句更短:

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

应要求提供更多说明

python 不支持(element1 and element2 ) in mylist 的语法,in 关键字一次只能应用于一个元素,因此您必须使用element1 in list and element2 in mylist,发生的情况是python 将(element1 and element2 ) 视为单独的语句, 将其评估为布尔值,然后检查生成的布尔值是否存在于 mylist 中。

【讨论】:

  • 既然你有两次"word1" in test_dash_sentence,你可以合并做更短的。
  • 请注意,if ("word1" and "word2") or ("word1" and "word3") in test_dash_sentence 被评估为 if (True and True) or ...,其评估结果为 True
  • 感谢 @adirabargil 提供的详细信息,我给了你 1 个赞,你的回答解决了主要问题,但是我担心将其应用于大量数据会使其很难阅读和维护
  • 如果您将展示数据如何扩展的演示,我们可以找到一种更具可读性和通用性的方法,但对于您当前的示例,我已尽力...我认为这是一个合适的答案...无论如何,如果条件满足,@azro 答案可能会满足您的需求...
  • 确实,您已经完美地回答了主要问题,并且很快,我感谢您。人们也很少考虑它,但如果它看起来与您相关,感谢您支持这个问题。
【解决方案2】:

你必须对每个单词重复in test_dash_sentenceword1 的简化在两个子句中,你得到

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

你也可以使用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")

对于多个可能需要的单词,每个单词都有其可选单词(一个必须匹配),您可以使用 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")

【讨论】:

  • 对不起,你能解释一下w在你的第二部分中代表什么吗?我觉得很混乱
  • @Ced 是一个本地函数,表示数组中的单词,requiredany_of,它用于测试w in test_dash_sentence
  • 好的,谢谢您的澄清,您将如何安排第二部分,但对于大量数据,您将如何安排第二部分,例如,我有 3 个必需的单词,每个单词都有唯一的 4对应的可选词(都是各自必填词唯一的)
  • 谢谢,但是在编辑中你使用了requiredany_of,而只有一个dict 叫做words,所以我看不出它是如何工作的,我错过了什么吗?
  • @Ced 他们来自for required, any_of in words.items()
【解决方案3】:

您的布尔条件不正确。此外,"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")

【讨论】:

  • 感谢您的回答,欢迎来到 StackOverflow!如果它似乎与您相关,请不要犹豫也支持这个问题! :)
猜你喜欢
  • 2012-09-03
  • 2022-10-01
  • 2017-01-05
  • 2015-11-28
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
相关资源
最近更新 更多