2015-04-03 318 views
0

我需要檢查一個字符串是否包含列表的任何元素。我目前使用這種方法:Python - 檢查字符串是否包含列表中的任何元素

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) 

這裏的問題是,它給輸出:

the english sentence is: the dogs fur is black and white 
The sentence is english and contains the words: 
['the', 'a', 'and', 'it'] 
>>> 

正如你可以看到「a」和「它」不應該存在。我如何搜索,以便它只會列出單個單詞,而不是單詞的一部分?我願意使用普通的Python代碼或正則表達式(儘管我對Python和正則表達式都很新,所以請不要太複雜)謝謝。

+0

這裏沒有任何正則表達式 - 這只是字符串操作。正則表達式是一種針對字符串提供匹配模式的非常具體的方式,如果您正在使用它們,您將使用're'模塊。 – geoelectric 2015-04-03 20:38:34

+1

順便說一句,值得注意的是,所有這些解決方案(包括我的)只有在沒有標點符號時纔有效。任何標點符號都將看起來像它旁邊的單詞的一部分,並使您的比較失敗。 如果你開始包括標點符號,你需要一些策略來刪除或忽略它。一種策略是針對整個句子字符串使用正則表達式,在每個單詞的任一側使用'\ b'來搜索。 – geoelectric 2015-04-03 20:51:10

回答

5

它找到了這兩個詞,因爲它們分別是「黑色」和「白色」的子字符串。當你將「in」應用於一個字符串時,它只是查找字符的子字符串。

嘗試:

engSentenceWords = engSentence.split() 

後來,

if w in engSentenceWords: 

,其將原來的句子翻譯成單個單詞的列表,然後對整個字值檢查。

0
words = set(engSentence.split()).intersection(set(engWords)) 
if words: 
    print("The sentence is english and contains the words: ") 
    print(words) 

將engSentence拆分爲列表中的標記,將其轉換爲集合,將engWords轉換爲集合並找到交集(公共重疊)。然後檢查它是否非空,如果是,打印出找到的單詞。

0

或者更簡單,添加一個空格,以你的句子,你的搜索詞:

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 
engSentence += " " 

for w in engWords: 
    if "%s " % w in engSentence: 
     isEnglish = 1 
     engWords2.append(w) 

if isEnglish == 1: 
    print("The sentence is english and contains the words: ") 
    print(engWords2) 

輸出爲:

the english sentence is: the dogs fur is black and white 
The sentence is english and contains the words: 
['the', 'and'] 
0

您可能需要使用正則表達式匹配。嘗試類似以下

import re 

match_list = ['foo', 'bar', 'eggs', 'lamp', 'owls'] 
match_str = 'owls are not what they seem' 
match_regex = re.compile('^.*({1}).*$'.format('|'.join(match_list))) 

if match_regex.match(match_str): 
    print('We have a match.') 

re文檔上python.org瞭解詳情。

相關問題