Python -从列表中的每个字符串中删除目标单词



我有一个字符串列表,例如

my_list = ['this is a string', 'this is also a string', 'another String']

我还有一个要从列表中每个字符串中删除的单词列表

remove = ['string', 'is']

我想从my_list中移除remove中的字符串。

我尝试遍历每个列表

new_list = []
for i in my_list:
for word in remove:
x = i.replace(word, "")
new_list.append(x)

但这只是返回每个原始句子。

您正在获取列表中的每个字符串,并分别删除每个单词,然后将它们附加到new_list。相反,您需要做的是删除这些特定的单词,然后将其添加到new_list中。这可以简单地通过重新组织

来完成。
new_list = []
for i in my_list:
x = i
for word in remove:
x = x.replace(word, "")
new_list.append(x)

但是,这将删除单词内的出现,而不仅仅是整个单词。只删除整个单词可以通过一些更多的逻辑来完成,例如

new_list = []
for i in my_list:
x = i.split()
new_list.append(" ".join(a if a not in remove else '' for a in x))

这个有点复杂,但它将每个字符串分割成一个列表,并使用列表推导形成一个新列表,其中包含所有要删除的单词,然后用一个空格将它们连接在一起。这也可以通过地图来实现。注意,这将导致删除的单词出现双空格,这可以通过添加

来弥补。
" ".join(a if a not in remove else '' for a in x)).replace("  ", " ")

沿着相同的路线重新实现您的解决方案,只需进行一些调整即可获得正确的结果:

  • 降低注释短语的大小写,因此删除诸如"String"仍然会匹配。
  • 替换时在移除字前加空格,避免替换"is"这个,";对于exampl.e

例子:

my_list = ['this is a string', 'this is also a string', 'another String']
remove = ['string', 'is']
annotated_list = []
for phrase in my_list:
annotated_phrase = phrase.casefold()
for pattern in remove:
annotated_phrase = annotated_phrase.replace(" " + pattern, "")
annotated_list.append(annotated_phrase)
print(annotated_list)

输出:

['this a', 'this also a', 'another']
l1=""
l2=[]
my_list = ['this is a string', 'this is also a string', 'another String']
remove = ['string', 'is']
for i in my_list:
l1=""
for j in i.split():
if j not in remove:
l1=l1+" " +j
l2+=[l1]        

print(l2)

您的代码将输出为['this is a ', 'th a string', 'this is also a ', 'th also a string', 'another String', 'another String'](每个单词中的is也被删除,这是不可取的)。您可以使用.split(),如上所示。

输出将是:

[' this a', ' this also a', ' another String']

编辑:

要去掉列表中每个元素中的空格,可以运行for循环并使用.lstrip()

运行你的代码,我得到了这个:['this is a ', 'th a string', 'this is also a ', 'th also a string', 'another String', 'another String']

字被删除,但由于您对每个字符串迭代remove两次,因此您得到了原始字符串的两倍。

如果您实际删除单词,即'is'中的'is'而不是'this',我建议使用正则表达式。

import re
my_list = ['this is a string', 'this is also a string', 'another String']
pattern = re.compile(r's*b(string|is)b|b(string|is)bs*')
new_list = [pattern.sub("", s) for s in my_list]
print(new_list)

将输出保存在同一列表中:

for rem in remove:
for i,str in enumerate(my_list):
if rem in str:
str = str.replace(rem, '')
my_list[i]=str
print(my_list)

输出:['th a ', 'th also a ', 'another String']

这应该可以完成工作:

input_string = 'this is a string, this is also a string, another String'
my_list = list(input_string.split())
remove = ['string', 'is']
remove_with_comma = [str(i) + ',' for i in remove]
correct_words = my_list[:]
for word in my_list:
if word in remove:
correct_words.remove(word)
elif word in remove_with_comma:
correct_words[correct_words.index(word)] = ','
print(' '.join(correct_words))

输出:'this a , this also a , another String'

最新更新