在“python”中对同一个变量使用多个条件
Using multiple conditions for the same variable in `python`
我的数据如下所示(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 列中的两个条件,则单词 "TRUE" 打印在第 5 列中,否则,单词 "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
。我查看了 here and here 以寻求解决问题的建议,代码仅适用于一个条件(例如将所有 'positive' 和 '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"
这个怎么样:
print bookID, ID, oneScore, twoScore, (oneScore == "positive" and twoScore > 0) \
or (oneScore == "null" and twoScore == 0) \
or (oneScore == "negative" and twoScore < 0)
听起来您想要 or
,而不是嵌套的 if
语句。您正在测试的所有条件都不可能同时为真,因此嵌套的 if
s(在这种情况下像 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
。
你应该使用 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)
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 列,制表符分隔):
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 列中的两个条件,则单词 "TRUE" 打印在第 5 列中,否则,单词 "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
。我查看了 here and here 以寻求解决问题的建议,代码仅适用于一个条件(例如将所有 'positive' 和 '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"
这个怎么样:
print bookID, ID, oneScore, twoScore, (oneScore == "positive" and twoScore > 0) \
or (oneScore == "null" and twoScore == 0) \
or (oneScore == "negative" and twoScore < 0)
听起来您想要 or
,而不是嵌套的 if
语句。您正在测试的所有条件都不可能同时为真,因此嵌套的 if
s(在这种情况下像 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
。
你应该使用 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)
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"
它应该将您的数据与逻辑分开并简化代码的维护。