我需要检查字符串是否包含列表的任何元素.我目前正在使用这种方法:
engWords = ["the", "a", "and", "of", "be", "that", "have", "it", "for", "not"]
engSentence = "the dogs fur is black and white"
print("the english sentence is: " + engSentence)
engWords2 = []
isEnglish = 0
for w in engWords:
if w in engSentence:
isEnglish = 1
engWords2.append(w)
if isEnglish == 1:
print("The sentence is english and contains the words: ")
print(engWords2)
Run Code Online (Sandbox Code Playgroud)
这个问题是它给出了输出:
the english sentence is: the dogs fur is black and white
The sentence is english and contains the words:
['the', 'a', 'and', 'it']
>>>
Run Code Online (Sandbox Code Playgroud)
你可以看到'a'和'it'不应该存在.我如何搜索,以便它只列出单个单词,而不是单词的一部分?我对使用普通python代码或正则表达式的任何想法持开放态度(虽然我对python和regex都很新,所以请不要太复杂)谢谢.
小智 5
它找到了这两个词,因为它们分别是"黑色"和"白色"的子串.将"in"应用于字符串时,它只查找字符的子字符串.
尝试:
engSentenceWords = engSentence.split()
Run Code Online (Sandbox Code Playgroud)
然后,
if w in engSentenceWords:
Run Code Online (Sandbox Code Playgroud)
将原始句子分成单个单词列表,然后检查整个单词值.