使用方法:虽然不在

Cin*_*bit 9 python while-loop

我正在尝试检查列表是否没有成员作为布尔运算符AND,OR,NOT.

我用:

while ('AND' and 'OR' and 'NOT') not in list:
  print 'No boolean operator'
Run Code Online (Sandbox Code Playgroud)

但是,当我的输入为:时a1 c2 OR c3 AND,它会输出'No boolean operator',这意味着通过使用上面的循环句子,该列表被认为是没有布尔运算符.

希望有人可以帮忙纠正.

谢谢,辛迪

gah*_*ooa 7

sets如果您有任何数据量,使用将快速尖叫

如果您愿意使用集合,则可以使用该isdisjoint()方法检查运算符列表与其他列表之间的交集是否为空.

MyOper = set(['AND', 'OR', 'NOT'])
MyList = set(['c1', 'c2', 'NOT', 'c3'])

while not MyList.isdisjoint(MyOper):
    print "No boolean Operator"
Run Code Online (Sandbox Code Playgroud)

http://docs.python.org/library/stdtypes.html#set.isdisjoint


eta*_*ion 3

anding strings 并不像您想象的那样 - 使用 any 来检查列表中是否有任何字符串:

while not any(word in list_of_words for word in ['AND', 'OR', 'NOT']):
    print 'No boolean'
Run Code Online (Sandbox Code Playgroud)

另外,如果您想要一个简单的检查, anif可能比while...更适合