[英]How to check if there is an entry that exists in an array/list in Python?
[英]Check if list entry exists in other list
我想檢查一個列表中的列表條目是否存在於另一個字符串列表中。 在此之后,我想從字符串中刪除匹配項。 在此處查看示例:
list1 = ["MyFirm Geographical Enablement Framework", "MyFirm Multiresource Scheduling",
"MyFirm SuccessFactors Recruiting"]
list2 = ["Install MyFirm SuccessFactors Recruiting in the right way",
"Get MyFirm Geographical Enablement Framework to work"]
為此,我想創建一個新列表,其中包含列表 2 中的所有單詞,但列表 1 中的單詞除外。因此,請參見此處的示例 output:
new_list = ["Install in the right way", "Get to work"]
我的第一種方法是這樣的,但它不起作用:
new_list = []
for element in list1:
if element.split() in list2:
result = list2.remove(element)
new_list.append(result)
有人對此有解決方案嗎?
創建一set
單詞以從 list1 中所有單詞的串聯中排除。 然后使用該集合過濾list2
中的句子單詞:
excluded = set(" ".join(list1).split())
new_list = [" ".join(word for word in sentence.split() if word not in excluded)
for sentence in list2]
print(new_list) # -> ['Install in the right way', 'Get to work']
如果list1
的任何項目(比如j
)在該項目( i
)內,您需要檢查list2
的每個項目(比如i
)。 如果在里面,找到它開始的索引,添加長度以獲得結束點,append 到new_list
然后跳出內部循環。
這就是你需要的:
new_list = []
for i in list2:
for j in list1:
if j in i:
new_list.append(i[:i.index(j)] + i[i.index(j) + len(j):])
break
print(new_list)
# ['Install in the right way', 'Get to work']
注意到中間的額外空間了嗎? 為此,您可能必須在連接時去除空格並添加一個額外的空格以獲得所需的結果。 對於將第 6 行更改為此:
new_list.append(i[:i.index(j)].rstrip() + " " + i[i.index(j) + len(j):].lstrip())
split
每個字符串以獲取單詞列表,然后重新加入list2
中的字符串,同時從list1
中過濾掉組合單詞列表中的字符串:
>>> [' '.join(w for w in s.split() if w not in {w for s in list1 for w in s.split()}) for s in list2]
['Install in the right way', 'Get to work']
我認為使用集合是一個很好的選擇:(僅當您對無序的結果感到滿意時)
list1 = ["MyFirm Geographical Enablement Framework", "MyFirm Multiresource Scheduling", "MyFirm SuccessFactors Recruiting"]
list2 = ["Install MyFirm SuccessFactors Recruiting in the right way", "Get MyFirm Geographical Enablement Framework to work"]
res = []
full_list1 = ' '.join(list1) # String
full_set1 = set(full_list1.split(' '))
for elem2 in list2:
set2 = set(elem2.split(' '))
tmp_diff = set2.difference(full_set1)
res.append(' '.join(list(tmp_diff)))
res
將是您想要的結果(列表)
另一種方式:
new_list = []
for it in list2:
for r_st in list1:
it = it.replace(r_st, '').strip()
new_list.append(it.replace(' ', ' '))
print(new_list)
>>>
new_list=['Install in the right way', 'Get to work']
我將首先從第一個列表創建一個查找列表,然后遍歷第二個列表中的單詞以消除第一個列表中出現的任何單詞。
lookup = ' '.join(list1).split()
new_list = []
for sentence in list2:
sentence = ' '.join([word for word in sentence.split() if word not in lookup])
new_list.append(sentence)
print(new_list)
# ['Install in the right way', 'Get to work']
我將用正則表達式解決答案 a:通過將它們與'|'
連接來制作受限短語的模式 ; 用''
代替所有字符串中的受限短語。
import re
a = ["MyFirm Geographical Enablement Framework",
"MyFirm Multiresource Scheduling", "MyFirm SuccessFactors Recruiting"]
b = ["Install MyFirm SuccessFactors Recruiting in the right way",
"Get MyFirm Geographical Enablement Framework to work"]
pattern = '|'.join('({})'.format(s) for s in a)
#print(pattern)
q = [re.sub(pattern,'',s) for s in b]
#print(q)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.