我想知道是否有办法在 python 或类似的东西中执行以下操作:
wordList = ['hello', 'hi', 'who', 'what']
message = "I don't know what you mean"
if any item of wordList is in message :
#Do whatever
else:
#Don't do whatever
很抱歉,如果我没有提供足够的信息,这是我关于 stackoverflow 的第一个问题。
我想知道是否有办法在 python 或类似的东西中执行以下操作:
wordList = ['hello', 'hi', 'who', 'what']
message = "I don't know what you mean"
if any item of wordList is in message :
#Do whatever
else:
#Don't do whatever
很抱歉,如果我没有提供足够的信息,这是我关于 stackoverflow 的第一个问题。
这是您想要的示例
wordsInMessage = [words for words in wordList if words in message]
基本上如果你想让我打破这个
wordList = ['hello', 'hi', 'who', 'what']
message = "I don't know what you mean"
#create list where similar words will go
wordsInMessage = []
#iterate through wordList
for words in wordList:
#if any way is in message
if words in message:
#put that word in the list created above
wordsInMessage.append(words)