在`python`中为同一个变量使用多个条件

时间:2015-09-09 13:44:50

标签: python python-2.7 if-statement conditional

我的数据类似于以下内容(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

使用这些数据,我需要编写一个条件语句,如果满足Cols 3和4中的两个条件,则单词" TRUE"在第5栏中打印,如果没有,则单词" FALSE"打印出来。

试图筑巢" IF"使用python的语句,我写了以下代码:

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"

此代码的结果是&#34; FALSE&#34;被分配给所有行,如下所示:

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以获得解决问题的建议,而且代码仅使用一个条件(例如标记所有&#39;肯定&#39;和&#39; x&gt; ; 0&#39;正确为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"

4 个答案:

答案 0 :(得分:4)

听起来你想要or,而不是嵌套的if语句。对于您同时测试的所有条件,它永远不可能实现,因此嵌套的if(在此上下文中类似于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

答案 1 :(得分:0)

这个怎么样:

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

答案 2 :(得分:0)

你应该使用if-elif而不是嵌套if if从你的代码中,它永远不会打印&#39; TRUE&#39;

正确的逻辑应该是这样的

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"

答案 3 :(得分:0)

如下:

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"

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