我正在尝试根据另一个子字符串列表对一个字符串列表进行子集设置。如果字符串在list2中包含子字符串,我想删除list1中的字符串。
list1 = ['lunch time', 'sandwich shop', 'starts at noon','grocery store']
list2 = ['lunch','noon']
我想要的输出:
output = ['sandwich shop','grocery store']
答案 0 :(得分:2)
使用正则表达式。
例如:
import re
list1 = ['lunch time', 'sandwich shop', 'starts at noon','grocery store']
list2 = ['lunch','noon']
pattern = re.compile(r"|".join(list2))
print([i for i in list1 if not pattern.search(i)])
输出:
['sandwich shop', 'grocery store']
答案 1 :(得分:1)
一种方法是对list1
的副本进行迭代,如果该字符串包含list2
的子字符串,则从其中删除该字符串
list1 = ['lunch time', 'sandwich shop', 'starts at noon','grocery store']
list2 = ['lunch','noon']
#Iterate on copy of list1
for item1 in list1[:]:
#If substring is present, remove string from list
for item2 in list2:
if item2 in item1:
list1.remove(item1)
print(list1)
另一种方法是找到匹配的子字符串,然后将结果与实际列表相减
list1 = ['lunch time', 'sandwich shop', 'starts at noon','grocery store']
list2 = ['lunch','noon']
#List of strings where the substrings are contained
result = [item1 for item1 in list1 for item2 in list2 if item2 in item1 ]
#List of strings where the substrings are not contained, found by set difference between original list and the list above
print(list(set(list1) - set(result)))
两种情况下的输出将相同
['grocery store', 'sandwich shop']
答案 2 :(得分:1)
有很多不同的方法可以做到这一点。这是我的方法(可能不是最好的方法)。
list1 = ['lunch time', 'sandwich shop', 'starts at noon','grocery store']
list2 = ['lunch','noon']
list3 = [x for x in list1 if len(set(list2) & set(x.split())) == 0]
print(list3)
给你:
['sandwich shop', 'grocery store']
发生了什么事?
split()
将项目转换为单词数组。 len()
计算有多少相似。