[英]check if string follows specific list of strings in python regex
我有一個字符串列表,我稱之為“text_sentences”類型
["bla bla bla", "yada yada yada","foo boo, foo"...]
然后我有一個特定字符串(單詞)的列表,我必須使用它來識別我的text_sentences
中的元素(句子),我稱之為“單詞”
words=["word1", "word2",..]
我的目標是根據words
識別text_sentences
中的句子,即如果一個句子至少包含 words 中的一個words
,則該句子( text_sentences
的元素)將被放入一個新列表中,說它“匹配”。 如果沒有,請將其放入名為“不匹配”的列表中。 我可以用類似的東西重現這個
matched=[]
unmatched_sent=[]
for j in range(len(text_sentences)):
if any(s in text_sentences[j] for s in words):
matched.append(text_sentences[j])
else:
unmatched.append(text_sentences[j])
但是:這只是我需要執行的過程的一個步驟。 事實上,我也有一個類型的否定詞列表
negations=["no","not","none"]
它的用途如下:如果 text_sentences 中的一個句子在 words 中至少包含一個單詞,那么該句子必須附加到matched
列表中; 但是,如果該句子中包含的單詞 from words
跟在negations
列表中的任何單詞之后,則該句子必須附加到unmatched
列表中。 如果句子中不包含任何單詞 from words
,那么它必須附加到unmatched
。 我怎樣才能一次完成這一切?
t = ["bla bla bla", "yada yada yada","foo boo, foo", "yoo no you are not in list"]
words = ["test", "bla", "yoo"]
negation = ["no","not","none"]
unmatched = []
matched = []
for i in words:
for j in t:
if i in j:
matched.append(j)
for l in t:
if l not in matched and l not in unmatched:
unmatched.append(l)
for m in negation:
for k in matched:
if m in k:
matched.remove(k)
print(unmatched)
print(matched)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.