Python bool 比较逻辑错误

标签 python boolean-logic

我正在使用 Python 执行一些数据验证和清理,并且在执行的一些 bool 比较中遇到了逻辑错误。我有一个需要清理的查找表,因此我正在比较范围值以确认值集之间没有重叠。所有列表值均从 CSV 文件输入。

代码:

print >>f2, "Filter logic II for " + myKey[0] + " " + myKey[1] + " "  + myKey[2] + ":"
print >>f2, "outerRow[3] " + outerRow[3] + " >= innerRow[3] " + innerRow[3] + " and outerRow[3] " + outerRow[3] + " <= innerRow[4]" + innerRow[4] + " OR outerRow[4] " + outerRow[4] + " >= innerRow[3] " + innerRow[3] + " and outerRow[4] " + outerRow[4] + " <= innerRow[4]" + innerRow[4]
if ((outerRow[3] >= innerRow[3]) and (outerRow[3] <= innerRow[4])) or ((outerRow[4] >= innerRow[3]) and (outerRow[4] <= innerRow[4])):
    Test2 = True
    print >>f2, "Filter logic II = True"
else:
    Test1 = False
    print >>f2, "Filter logic II = False"

第一次运行后会产生结果:

*Filter logic II for XYZ KEY123 PRE:  
outerRow[3] 0 >= innerRow[3] 80 and outerRow[3] 0 <= innerRow[4]100 OR outerRow[4] 79 >= innerRow[3] 80 and outerRow[4] 79 <= innerRow[4]100  
Filter logic II = False*

但是第二次运行后出现这个(意外的)结果:

*Filter logic II for 080570BD 1998 VA PRE:  
outerRow[3] 80 >= innerRow[3] 0 and outerRow[3] 80 <= innerRow[4]79 OR outerRow[4] 100 >= innerRow[3] 0 and outerRow[4] 100 <= innerRow[4]79  
Filter logic II = True*

我在逻辑比较中哪里出错了?我已经盯着这个有点太久了,想看看我是否可以从全局网络获得帮助。

最佳答案

尝试将数据转换为int。这是来自交互式 Python shell:

>>> "100" <= "79"
True
>>> 100 <= 79
False
>>> int("100") <= int("79")
False

关于Python bool 比较逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9119625/

相关文章:

python - Eclipse/PyDev 中良好的调用层次结构

python - 如何为 python 3.7 正确安装 Flask 以运行这个 github 项目?

python - SCons 构建多个环境

math - 计算 bool 表达式的简化乘积和

c++ - 在 C++ 中,为什么 true && true ||假&&假==真?

haskell - 在Haskell中为逻辑表达式生成真值表

Python解析dirs,删除子目录树

python - Google App Engine 查询限制和超时

c# - 我怎样才能最好地检查 A xor B 是否为空?

c - C 程序中的条件未给出所需的结果