Эй, я выполнял упражнение в 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' in:
if keyword.lower() in normalized:
и изменил его на:
if keyword.lower() == normalized:
и не получилправильный ответ.Мой вопрос почему?в чем разница между двумя утверждениями?Если вы следуете коду, идея состоит в том, чтобы найти определенное ключевое слово в документе.Итак, ключевое слово == слово в документе.
(я могу предоставить упражнение (контекст?), Но мне это не важно, поскольку мой вопрос общий.)
Спасибо.