我试图删除列表中的特定单词。假设我有以下示例:
a= ['you are here','you are there','where are you','what is that']
b = ['you','what is']
所需输出应如下:
['are here', 'are there', 'where are', 'that']
我为该任务创建了以下代码:
import re
def _find_word_and_remove(w,strings):
"""
w:(string)
strings:(string)
"""
temp= re.sub(r'\b({0})\b'.format(w),'',strings).strip()# removes word from string
return re.sub("\s{1,}", " ", temp)# removes double spaces
def find_words_and_remove(words,strings):
"""
words:(list)
strings:(list)
"""
if len(words)==1:
return [_find_word_and_remove(words[0],word_a) for word_a in strings]
else:
temp =[_find_word_and_remove(words[0],word_a) for word_a in strings]
return find_words_and_remove(words[1:],temp)
find_words_and_remove(b,a)
>>> ['are here', 'are there', 'where are', 'that']
似乎我对这个任务使用递归过于复杂化了。有没有更简单易读的方法来完成这个任务?你知道吗
您可以使用列表理解:
只有当
b
中只有一个单词时,这才有效,但是由于您的编辑和注释,我现在知道您确实需要_find_word_and_remove()
。您的递归方式并不太糟糕,但如果您不希望递归,请执行以下操作:简单的方法是使用regex:
给你:
相关问题 更多 >
编程相关推荐