逻辑表达式:为什么“str1 in str2 或 str2 not in str1”为我的打印语句返回 True?

问题描述

首先,我为愚蠢的问题或我帖子中的任何错误道歉! (这是我的第一篇文章!)

其次,我试图在 Stack Overflow 和 RealPython 网站(逻辑表达式)上查找这个问题的答案,但我似乎仍然无法理解为什么这段代码会返回“真”到最后一个(未注释的)函数调用

我正在尝试回答电子书“使用 Python 进行计算和编程简介”中的以下问题 -

“编写一个函数 isIn,它接受两个字符串作为参数,如果其中一个字符串出现在另一个字符串中的任何位置,则返回 True,否则返回 False。提示:您可能想要使用内置的 str 操作。”

如果有人能解释(用外行的话)为什么下面的代码在最后一个函数调用中返回“True”,我将非常感激 - 再次为这个愚蠢的问题道歉!

def isIn(string1,string2):
    """Input two strings 
       Returns 'True' if either strings are within each other
    """

    if string1 in string2 or string2 not in string1:
        return True
    else:
        return False

# isIn('hello','hello')           # Prints True
# isIn('hello','hello world')     # Prints True   
isIn('hello world','world hello') # Prints True

解决方法

为什么你的函数返回 True

if 语句 if string1 in string2 or string2 not in string1 由 3 部分组成:

  1. string1 in string2
  2. or
  3. string2 not in string1

你有:

string1 = 'hello world'
string2 = 'world hello'
  • 第 1 部分 (string1 in string2):

    它的计算结果为 False,因为 'hello world' 不在 'world hello' 中

  • 第 3 部分 (string2 not in string1):

    计算结果为 True,因为 'world hello' 实际上不存在于 'hello world'

  • 第 2 部分,or

    or 会给你:

    • True 如果至少有一个表达式的计算结果为 True
    • False 如果所有表达式的计算结果都为 False

所以你得到 True

但如果你使用了 and,你会得到 False

如果有时您有疑问,请尝试以下打印:

# or:
print(True or True) # True
print(True or False) # True
print(False or False) # False

# and:
print(True and True) # True
print(True and False) # False
print(False and False) # false

回答您的评论:

不,“hello world”不在“world hello”中 那么,'world hello' 是什么?

  • 'world'、'hello'、' '(空格)和 ''(空字符串)。
  • 以及所有可能的子字符串(源中的字符和连续字符 字符串,例如 'h'、'he'、'hel'、'wo' 等)。请注意,实际上所有项目 'world'、'hello'、' ' 和 '' 都是 :-)

所以,所有这些都被评估为真:

# string2 = 'world hello'
'world' in string2
'hello' in string2
' ' in string2
'' in string2
'h' in string2
'e' in string2
'llo' in string2
'llo 'wo' in string2
# etc.

在计算机科学中,字符串是一个字符序列。 每个子序列都是一个子串。

所以现在,您应该对什么是字符串,什么是子字符串有了更好的了解,如果您有兴趣,可以/应该在互联网上搜索一些信息。

那么,in 表达式是什么? 实际上,in 表达式在处理字符串时会告诉您在另一个字符串中搜索的字符串的字符是否是该 字符串子字符串 em> 与否。

总而言之,字符序列“hello world”不在字符序列“world hello”中。

,

您的 If 语句正在检查 2 个条件:

  1. string1 in string2 :这是错误的,因为 string1 是 'hello world' 并且它不存在于 string2 中。如果 string1 是 'hello' 或 'world' 或 'world hello' 或 ' ',它将返回 True,因为所有这些都存在于 string2 中。

  2. string2 not in string1 :它会检查 string2 是否不存在于 string1 你的函数因为这种情况而返回 True,'world hello' is not present in string1

,

加上@NoobCoder 的答案,您的程序由于第二个条件返回 true:

"or string2 not in string1" 返回 TRUE,因为第二个字符串不在第一个字符串中。

然后,根据您的条件:Cond1 OR Cond2,如果其中任何一个返回 TRUE,您的最终答案将为 TRUE。

,

嘿 :D 我认为你可以使用 for 循环来检查 string1 是否包含 string2 中的单词 Idk 如何将 python 代码放在这里,因为我也是新手:\


但整个事情就像


对于 b(字符串、列表等)中的 a(变量):


    from tensorflow.python.client import device_lib
    print(device_lib.list_local_devices()) # list of DeviceAttributes

    print(tf.__version__)```
```[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 16122061786637476915,name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 8658159843921049879
physical_device_desc: "device: XLA_CPU device"
]

1.14.0

然后编码其余部分