在python中从文本列表中删除短语列表

2024-04-26 10:25:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图删除列表中的特定单词。假设我有以下示例:

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']

似乎我对这个任务使用递归过于复杂化了。有没有更简单易读的方法来完成这个任务?你知道吗


Tags: andreyoustringthatherefindwhere
2条回答

您可以使用列表理解:

def find_words_and_remove(words, strings):
    return [" ".join(word for word in string.split() if word not in words) for string in strings]

只有当b中只有一个单词时,这才有效,但是由于您的编辑和注释,我现在知道您确实需要_find_word_and_remove()。您的递归方式并不太糟糕,但如果您不希望递归,请执行以下操作:

def find_words_and_remove(words, strings):
    strings_copy = strings[:]
    for i, word in enumerate(words):
        for string in strings:
            strings_copy[i] = _find_word_and_remove(word, string)
    return strings_copy

简单的方法是使用regex:

import re

a= ['you are here','you are there','where are you','what is that']
b = ['you','what is']

给你:

def find_words_and_remove(b,a):
    return [ re.sub("|".join(b), "", x).strip() if len(re.sub("|".join(b), "", x).strip().split(" ")) < len(x.split(' ')) else x for x in a  ]

find_words_and_remove(b,a)
>> ['are here', 'are there', 'where are', 'that']

相关问题 更多 >