How can I split a string from every word in an array with the split method and then compare it to an array?

huangapple go评论78阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月6日 15:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定