Python - 正确使用 OR 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14228572/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:49:24  来源:igfitidea点击:

Python - Properly Use OR function

python

提问by Dustin

Possible Duplicate:
OR behaviour in python:

可能的重复:
python 中的 OR 行为:

Probably extremely basic, but I'm a bit stuck after searching around. To me, this line of code should result in it printing "ELSE" as the string does not contain either word. Obviously it's not that simple and can't seem to figure out why. I've made sure to split the string in to a list just to make things easier to search. What am I doing wrong?

可能非常基本,但在四处搜索后我有点卡住了。对我来说,这行代码应该导致它打印“ELSE”,因为字符串不包含任何一个单词。显然这不是那么简单,似乎无法弄清楚原因。我确保将字符串拆分为一个列表,只是为了使搜索更容易。我究竟做错了什么?

string = "Johnny Was Here Yesterday"
string = string.split()

if "Bob" or "Hello" in string:
    print "IF"
else:
    print "ELSE"

采纳答案by Volatility

The proper way is to do this:

正确的方法是这样做:

if 'Bob' in text or 'Hello' in text:
    print 'IF'
else:
    print 'ELSE'

The reason your code doesn't work is because Python evaluates that as:

您的代码不起作用的原因是因为 Python 将其评估为:

if ('Bob') or ('Hello' in text):

Because 'Bob'always evaluates to True, your function always prints 'IF'

因为'Bob'总是评估为True,您的函数总是打印'IF'

By the way, it's not good to use stror stringas a variable name, as it overwrites the very usefulbuiltin method str()or the library string.

顺便说一句,使用strstring作为变量名并不好,因为它覆盖了非常有用的内置方法str()或库string

回答by DanielB

In Python, non-empty string evaluate to True.

在 Python 中,非空字符串的计算结果为 True。

Your ifstatement is interpreted as follows:

您的if声明解释如下:

if ("Bob") or ("Hello" in string"):

Since "Bob"is a non empty string, the statement always evaluates to True.

由于"Bob"是非空字符串,因此该语句的计算结果始终为True

You should use something like

你应该使用类似的东西

if "Bob" in string or "Hello" in string:

or

或者

if any(map(lambda x: x in string, ["Bob", "Hello"]))

As noted in other answers, you may wish to reconsider using the variable name stringas it clashes with one of pythons standard libraries.

正如其他答案中所述,您可能希望重新考虑使用变量名称,string因为它与 python标准库之一发生冲突。

回答by Yuushi

The way it's written, there will be two tests; the first test is if "Bob", the second test is "Hello" in string. if stris always true for any non-empty string, so if "Bob"always evaluates to true.

它的编写方式将有两个测试;第一个测试是if "Bob",第二个测试是"Hello" in stringif str对于任何非空字符串if "Bob"始终为真,因此始终为真。

You can fix this by either using if ("Bob" in string) or ("Hello" in string):, or by using if any((x in string for x in ["Hello", "Bob"]):.

您可以通过使用if ("Bob" in string) or ("Hello" in string):或使用来解决此问题if any((x in string for x in ["Hello", "Bob"]):

回答by jinghli

"Bob" is a 3-length string, it is Truewhen it is in the cause.

“鲍勃”是一个 3 长的字符串,True它是在原因时。

>>> bool("Bob")
True
>>> bool("")
False

You should modify to:

您应该修改为:

if "Bob" in string or "Hello" in string:
    print "IF"
else:
    print "ELSE"