if 条件语句中 '==' 和 'in' 有什么区别?
What's the difference between '==' and 'in' in if conditional statements?
嘿,我在做一个 Kaggle 的练习,虽然我正确地解决了它,但我想看看 Kaggle 提供的解决方案。这里:
def word_search(documents, keyword):
# list to hold the indices of matching documents
indices = []
# Iterate through the indices (i) and elements (doc) of documents
for i, doc in enumerate(documents):
# Split the string doc into a list of words (according to whitespace)
tokens = doc.split()
# Make a transformed list where we 'normalize' each word to facilitate matching.
# Periods and commas are removed from the end of each word, and it's set to all lowercase.
normalized = [token.rstrip('.,').lower() for token in tokens]
# Is there a match? If so, update the list of matching indices.
if keyword.lower() in normalized:
indices.append(i)
return indices
doc_list = ["The Learn Python Challenge Casino.", "They bought a car", "Casinoville"]
word_search(doc_list, 'casino')
我采用了解决方案并将 'in' 更改为:
if keyword.lower() in normalized:
并将其更改为:
if keyword.lower() == normalized:
并没有得到正确答案。我的问题是为什么?这两个陈述有什么区别?如果按照代码来看,思路就是在文档中查找某个关键字。因此,关键字 == 文档中的单词。
(我可以提供练习(上下文?)但我不认为这很重要,因为我的问题是一般性问题。)
谢谢。
第一个语句 if keyword.lower() in normalized:
正在检查 keyword.lower()
字符串是否是列表 中 中的元素之一 normalized
。这是真的。
另一个语句 if keyword.lower() == normalized:
正在检查 keyword.lower()
字符串是否具有 与 normalized
列表相同的值。这是错误的。
"in" 关键字测试成员资格。我不太了解您的变量,但我假设您想要查找的是 "keyword" 变量是否是 "in" 规范化列表。在这里使用“==”就好像在说,"keyword" 变量是否等于规范化列表变量(如果您的关键字是一个字符串,而您的规范化列表是一个列表,那么它显然不是)
因为 normalized
是一个 list
,而 keyword.lower()
是一个 str
ing,这已经是一个区别了,str
ing 不能是等同于 list
,此 ==
运算符检查某物是否等于另一物,而 in
运算符检查某物是否包含另一物,演示:
>>> a=4
>>> b=4
>>> a==b
True
>>> a in b
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
a in b
TypeError: argument of type 'int' is not iterable
>>> a=4
>>> b=[1,4]
>>> a==b
False
>>> a in b
True
>>>
仅当两个元素之间存在完全匹配且它们具有相同的 dtype
时,使用 ==
才能得到 True
if keyword.lower() == normalized:
此处,keyword.lower() #String# 与规范化的#list#
不完全匹配
使用 in
将进行更轻松的搜索,其中左侧元素可以在右侧元素中的任何位置
if keyword.lower() in normalized:
这里,如果 keyword.lower() 在 normalized
中的任何地方被发现,它将 return 为真。
嘿,我在做一个 Kaggle 的练习,虽然我正确地解决了它,但我想看看 Kaggle 提供的解决方案。这里:
def word_search(documents, keyword):
# list to hold the indices of matching documents
indices = []
# Iterate through the indices (i) and elements (doc) of documents
for i, doc in enumerate(documents):
# Split the string doc into a list of words (according to whitespace)
tokens = doc.split()
# Make a transformed list where we 'normalize' each word to facilitate matching.
# Periods and commas are removed from the end of each word, and it's set to all lowercase.
normalized = [token.rstrip('.,').lower() for token in tokens]
# Is there a match? If so, update the list of matching indices.
if keyword.lower() in normalized:
indices.append(i)
return indices
doc_list = ["The Learn Python Challenge Casino.", "They bought a car", "Casinoville"]
word_search(doc_list, 'casino')
我采用了解决方案并将 'in' 更改为:
if keyword.lower() in normalized:
并将其更改为:
if keyword.lower() == normalized:
并没有得到正确答案。我的问题是为什么?这两个陈述有什么区别?如果按照代码来看,思路就是在文档中查找某个关键字。因此,关键字 == 文档中的单词。
(我可以提供练习(上下文?)但我不认为这很重要,因为我的问题是一般性问题。)
谢谢。
第一个语句 if keyword.lower() in normalized:
正在检查 keyword.lower()
字符串是否是列表 中 中的元素之一 normalized
。这是真的。
另一个语句 if keyword.lower() == normalized:
正在检查 keyword.lower()
字符串是否具有 与 normalized
列表相同的值。这是错误的。
"in" 关键字测试成员资格。我不太了解您的变量,但我假设您想要查找的是 "keyword" 变量是否是 "in" 规范化列表。在这里使用“==”就好像在说,"keyword" 变量是否等于规范化列表变量(如果您的关键字是一个字符串,而您的规范化列表是一个列表,那么它显然不是)
因为 normalized
是一个 list
,而 keyword.lower()
是一个 str
ing,这已经是一个区别了,str
ing 不能是等同于 list
,此 ==
运算符检查某物是否等于另一物,而 in
运算符检查某物是否包含另一物,演示:
>>> a=4
>>> b=4
>>> a==b
True
>>> a in b
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
a in b
TypeError: argument of type 'int' is not iterable
>>> a=4
>>> b=[1,4]
>>> a==b
False
>>> a in b
True
>>>
仅当两个元素之间存在完全匹配且它们具有相同的 dtype
时,使用==
才能得到 True
if keyword.lower() == normalized:
此处,keyword.lower() #String# 与规范化的#list#
不完全匹配使用 in
将进行更轻松的搜索,其中左侧元素可以在右侧元素中的任何位置
if keyword.lower() in normalized:
这里,如果 keyword.lower() 在 normalized
中的任何地方被发现,它将 return 为真。