我對python很陌生,試圖找到tweet是否有任何查找元素。使用字符串以任意順序匹配數組元素
例如,如果我能找到這個單詞貓,它應該匹配貓,也可以任意順序匹配可愛的小貓。但從我瞭解我無法找到解決方案。任何指導表示讚賞。
import re
lookup_table = ['cats', 'cute kittens', 'dog litter park']
tweets = ['that is a cute cat',
'kittens are cute',
'that is a cute kitten',
'that is a dog litter park',
'no wonder that dog park is bad']
for tweet in tweets:
lookup_found = None
print re.findall(r"(?=(" + '|'.join(lookup_table) + r"))", tweet.lower())
輸出
['cat']
[]
[]
['dog litter park']
[]
預期輸出:
that is a cute cat > cats
kittens are cute > cute kittens
this is a cute kitten > cute kittens
that is a dog litter park > dog litter park
no wonder that dog park is bad > dog litter park
?? ??使用單數形式。 –
你也應該告訴我們你實際需要的輸出。 –
@KarolyHorvath我不確定你的意思是 – user6083088