英文:
How can I split a string from every word in an array with the split method and then compare it to an array?
问题
I Was wondering how to make a string turn into an array of every word and I came across the .split()
function and that split it, but now I want to compare it to another array. How can I do that?
keywords = ["help", "where", "find"]
x = input("How can I help: ")
search = x.split(" ")
for word in search:
if word in keywords:
print("it looks like you need help with:", word)
else:
print("I did not understand")
print(word, "not found in keywords")
I dont really know what to do and any help would be appreciated
英文:
I Was wondering how to make a string turn into an array of every word and I came across the .split()
function and that split it, but now I want to compare it to another array. How can I do that?
keywords = ["help", "where", "find"]
x = input("How can I help: ")
search = x.split(" ")
for search in keywords:
if search == keywords:
print("it looks like you need help with: ", keywords)
else:
print("I did not understand")
print(search, " ", keywords)
I dont really know what to do and any help would be appreciated
答案1
得分: 2
以下是翻译好的内容:
你很可能在寻找这样的内容:
search = x.split(" ")
for kw in keywords: # 不要遮蔽 `search`
if kw in search: # 使用成员测试而不是相等测试
print("看起来你需要帮助:", kw)
else:
print("我不明白")
print(search, " ", keywords)
英文:
You are most likely looking for sth like this:
search = x.split(" ")
for kw in keywords: # don't shadow `search`
if kw in search: # do membership instead of equality test
print("it looks like you need help with: ", kw)
else:
print("I did not understand")
print(search, " ", keywords)
答案2
得分: 0
使用for (迭代变量) in 列表
时,不能使用已定义的变量名作为迭代变量!这个变量取值为你正在迭代的列表中的每个单词,依次循环。
希望能对你有所帮助!我尽量保持你的代码尽可能简单。如果你正在寻找更优化的解决方案,请使用user2390182的答案。
如有需要,请随时提出更多问题。
英文:
When using for (iterating variable) in list
, you can't use an already defined variable name for the iterating variable ! this variable takes the value of every word you're iterating through in your list, one after the other.
Hope this helps ! I tried keeping as much of your own code as simple as possible. If you're looking for a more optimized solution, use user2390182's answer.
Feel free to ask further questions if needed.
keywords = ["help", "where", "find"]
x = input("How can I help: ")
search = x.split(" ")
success = False
for word in search : #iterate through each word of variable 'search'
if word in keywords: #if the word we're currently iterating is in the list 'keywords' :
print("it looks like you need help with: ", word) #we show the word that triggered the 'if'
success = True #we take note of the fact we successfully helped the user
if not success:
print("I did not understand")
print(search, " ", keywords)
#the 'if' is located after the loop, outside of its scope.
#this way it only triggers once after the loop instead of triggering with each iteration of the loop
output :
How can I help: i want to find excalibur
it looks like you need help with: find
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论