lst = ['Copper', 'Wood', 'Glass', 'Metal']
string = 'Wood table with small glass center,little bit of metal'
keywords = ['small','little']
punc = '''!()-[]{};:'"\, <>./?@#$%^&*_~'''
for ele in string:
if ele in punc:
string = string.replace(ele, " ")
lst = [stringg.lower() for stringg in lst]
string = string.lower()
lst = [word for word in lst if word.lower() in string.lower()]
words_lst = string.split(' ')
final = []
count = 0
for elem in lst:
count = 0
index = words_lst.index(elem)
slice_index = index - 4 if index - 4 >= 0 else 0
range_lst = words_lst[slice_index:index + 1]
for keyword in keywords:
if keyword not in range_lst and elem not in final:
count += 1
if count == len(keywords):
final.append(elem)
我的理解:我从您的问题中了解到,您试图从列表中删除紧跟在
'small'
和'little'
之后的值代码:
输出:
相关问题 更多 >
编程相关推荐