Python布尔比较逻辑错误

时间:2012-02-02 20:37:07

标签: python boolean-logic

我正在使用Python执行一些数据验证和清理,并且在我正在执行的一些布尔比较中遇到了逻辑错误。我有一个需要清理的查找表,所以我比较范围值以确认值集之间没有重叠。所有列表值都已从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*

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

1 个答案:

答案 0 :(得分:3)

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

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