Python 使用方法:不在时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4908666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use: while not in
提问by CindyRabbit
I'm trying to check if a list has no member as boolean operator AND, OR, NOT.
我正在尝试检查列表是否没有成员作为布尔运算符 AND、OR、NOT。
I use:
我用:
while ('AND' and 'OR' and 'NOT') not in list:
print 'No boolean operator'
However, when my input is: a1 c2 OR c3 AND, it prints 'No boolean operator', which means this list is considered no boolean operator in it by using above loop sentence.
但是,当我的输入是: 时a1 c2 OR c3 AND,它会打印“No boolean operator”,这意味着通过使用上述循环语句,该列表中没有布尔运算符。
Hope somebody can help to correct.
希望有人能帮忙纠正。
Thanks, Cindy
谢谢,辛迪
回答by etarion
anding strings does not do what you think it does - use any to check if any of the strings are in the list:
anding 字符串不会做你认为它做的事情 - 使用 any 来检查列表中是否有任何字符串:
while not any(word in list_of_words for word in ['AND', 'OR', 'NOT']):
print 'No boolean'
Also, if you want a simple check, an ifmight be better suited than a while...
另外,如果你想要一个简单的检查,一个if可能比一个更适合while......
回答by gahooa
In your case, ('AND' and 'OR' and 'NOT')evaluates to "NOT", which may or may not be in your list...
在您的情况下,('AND' and 'OR' and 'NOT')评估为"NOT",这可能会或可能不会在您的列表中...
while 'AND' not in MyList and 'OR' not in MyList and 'NOT' not in MyList:
print 'No Boolean Operator'
回答by Ned Batchelder
The expression ('AND' and 'OR' and 'NOT')evaluates to 'NOT', so you are testing whether the list has NOT or not.
表达式的('AND' and 'OR' and 'NOT')计算结果为'NOT',因此您正在测试列表是否为 NOT。
回答by eyquem
while not any( x in ('AND','OR','NOT') for x in list)
EDIT:
编辑:
thank you for the upvotes , but etarion's solution is better since it tests if the words AND, OR, NOT are in the list, that is to say 3 tests.
感谢您的投票,但 etarion 的解决方案更好,因为它测试单词 AND、OR、NOT 是否在列表中,也就是说 3 个测试。
Mine does as many tests as there are words in list.
我的测试与列表中的单词一样多。
EDIT2:
编辑2:
Also there is
还有
while not ('AND' in list,'OR' in list,'NOT' in list)==(False,False,False)
回答by Sven Marnach
The expression 'AND' and 'OR' and 'NOT'always evaluates to 'NOT', so you are effectively doing
表达式'AND' and 'OR' and 'NOT'始终计算为'NOT',因此您正在有效地执行
while 'NOT' not in some_list:
print 'No boolean operator'
You can either check separately for all of them
您可以单独检查所有这些
while ('AND' not in some_list and
'OR' not in some_list and
'NOT' not in some_list):
# whatever
or use sets
或使用集合
s = set(["AND", "OR", "NOT"])
while not s.intersection(some_list):
# whatever
回答by ChristopheD
If I understand the question correctly you are looking for something like this:
如果我正确理解这个问题,你正在寻找这样的东西:
>>> s = "a1 c2 OR c3 AND"
>>> boolops = ["AND", "OR", "NOT"]
>>> if not any(boolop in s for boolop in boolops):
... print "no boolean operator"
...
>>> s = "test"
>>> if not any(boolop in s for boolop in boolops):
... print "no boolean operator"
...
no boolean operator
>>>
回答by wisty
That's not how it works.
这不是它的工作原理。
This bit ('AND' and 'OR' and 'NOT')will evaluate as 'NOT'. So your code is equivalent to::
该位('AND' and 'OR' and 'NOT')将评估为'NOT'. 所以你的代码相当于:
while not 'NOT' in list: print 'No boolean operator'
虽然列表中不是“NOT”:打印“无布尔运算符”
You could try this:
你可以试试这个:
while not set('AND' and 'OR' and 'NOT').union(list): print 'No boolean operator'
虽然未设置('AND' 和 'OR' 和 'NOT')。union(列表):打印“无布尔运算符”
回答by gahooa
Using setswill be screaming fast if you have any volume of data
sets如果您有任何数据量,使用将很快
If you are willing to use sets, you have the isdisjoint()method which will check to see if the intersection between your operator list and your other list is empty.
如果您愿意使用集合,您可以使用该isdisjoint()方法来检查您的运营商列表和其他列表之间的交集是否为空。
MyOper = set(['AND', 'OR', 'NOT'])
MyList = set(['c1', 'c2', 'NOT', 'c3'])
while not MyList.isdisjoint(MyOper):
print "No boolean Operator"

