【问题标题】:Using multiple conditions for the same variable in `python`在`python`中对同一个变量使用多个条件
【发布时间】:2015-12-05 12:35:31
【问题描述】:

我的数据如下所示(4 列和制表符分隔):

AAA 123 null    0
AAA 124 null    1
BBB 234 null    0
CCC 235 negative    -2
CCC 345 negative    2
DDD 346 null    -1
EEE 456 positive    4
EEE 457 positive    0

使用这些数据,我需要编写一个条件语句,如果满足第 3 列和第 4 列中的两个条件,则在第 5 列打印单词“TRUE”,否则打印单词“FALSE” .

尝试使用python 嵌套“IF”语句,我编写了以下代码:

with open('infile.input', "r") as opened_file:
    for gLine in opened_file:
        print gLine
        oneID, twoID, oneScore, twoScore = gLine.split()
        if oneScore == "positive" and twoScore > 0:
            if oneScore == "null" and twoScore == 0:
                if oneScore == "neutral" and twoScore == 0:
                    if oneScore == "negative" and twoScore < 0:
                        print oneID, twoID, oneScore, twoScore, "TRUE"
        else:
            print oneID, twoID, oneScore, twoScore, "FALSE"

这段代码的结果是“FALSE”分配给所有的行,如下:

AAA 123 null    0   FALSE
AAA 124 null    1   FALSE
BBB 234 null    0   FALSE
CCC 235 negative    -2  FALSE
CCC 345 negative    2   FALSE
DDD 346 null    -1  FALSE
EEE 456 positive    4   FALSE
EEE 457 positive    0   FALSE

。我查看了herehere 以寻求解决问题的建议,并且代码仅适用于一种条件(例如,将所有“肯定”和“x>0”正确标记为 TRUE)。当我添加多个条件时,它无法达到我想要的结果,如下所示:

AAA 123 null    0   TRUE
AAA 124 null    1   FALSE
BBB 234 null    0   TRUE
CCC 235 negative    -2  TRUE
CCC 345 negative    2   FALSE
DDD 346 null    -1  FALSE
EEE 456 positive    4   TRUE
EEE 457 positive    0   FALSE

使用下面的建议,我尝试实现这一点,它只能正确找到第一个条件的情况。所有其他条件,无论它们是否为真,都被标记为假。我怎样才能让它识别所有 4 个条件?

if  ((oneScore == "positive" and twoScore > 0) or
         (oneScore == "null" and twoScore == 0) or
         (oneScore == "neutral" and twoScore == 0) or
         (oneScore == "negative" and twoScore < 0)):
        print oneID, twoID, oneScore, twoScore, "TRUE"
    else:
        print oneScore, twoScore, "FALSE"

【问题讨论】:

  • 你熟悉if ... elif ...这个概念吗?
  • 如果oneScore == "positive" oneScore == "null" 不可能是真的
  • 你需要用or链接条件,没有嵌套的ifs:if (a and b) or (c and d) or ...

标签: python python-2.7 if-statement conditional


【解决方案1】:

这个怎么样:

print bookID, ID, oneScore, twoScore, (oneScore == "positive" and twoScore > 0) \
    or (oneScore == "null" and twoScore == 0) \
    or (oneScore == "negative" and twoScore < 0)

【讨论】:

  • 这是非法的 pyhton 语法,不能简单地在下一行继续使用 or。要么将所有内容括在括号中,要么在行尾使用反斜杠作为续行标记。
【解决方案2】:

您应该使用 if-elif 而不是嵌套 if,从您的代码中,它永远不会打印 'TRUE'

正确的逻辑应该是这样的

if oneScore == 'positive' and int(twoScore) > 0:
     print bookID, ID, oneScore, twoScore, "TRUE"

elif (oneScore == 'neutral' or oneScore == 'null') and int(twoScore) == 0:
    print bookID, ID, oneScore, twoScore, "TRUE"

elif oneScore == 'negative' and int(twoScore) < 0:
    print bookID, ID, oneScore, twoScore, "TRUE"

else:
    print bookID, ID, oneScore, twoScore, "FALSE"

【讨论】:

  • 这样的解决方案会导致我在编辑的问题和更新的代码中提到的相同问题。见上文。
  • 我只是注意到'twoScore' 不是一个整数。这就是为什么它搞砸了条件评估。我认为您也必须将“int(twoScore)”放入条件中。
【解决方案3】:

类似的东西呢:

twoScore = int(twoScore)
cases = [
    oneScore == "positive" and twoScore > 0,
    oneScore == "null" and twoScore == 0,
    oneScore == "neutral" and twoScore == 0,
    oneScore == "negative" and twoScore < 0
]
state = any(cases) and "TRUE" or "FALSE"

它应该将您的数据与您的逻辑分开并简化代码的维护。

【讨论】:

    【解决方案4】:

    听起来您想要or,而不是嵌套的if 语句。您测试的所有条件都不可能同时为真,因此嵌套的ifs(在这种情况下类似于and)永远不会全部通过,让您的代码打印True .

    试试:

    if  ((oneScore == "positive" and twoScore > 0) or
         (oneScore == "null" and twoScore == 0) or
         (oneScore == "neutral" and twoScore == 0) or
         (oneScore == "negative" and twoScore < 0)):
        print bookID, ID, oneScore, twoScore, "TRUE"
    

    twoScore 的比较仍然会出现问题,因为在您从文件中读取的行之后,split 将是一个字符串。在进行比较之前,您需要在某个时间点调用int

    【讨论】:

    • 使用or 条件似乎只适用于第一个条件。这意味着在实施此提案时,它在满足条件时将第一个条件标记为 TRUE,将其他所有条件标记为 FALSE(无论它是否真的为假。)请参阅相关编辑。
    • 对不起,我没有读完所有内容。我创建了另一个变量intTwoScore = int(twoScore) 并替换了intTwoScore 逻辑中的所有twoScore,它就像一个魅力。
    猜你喜欢
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多