python:如何从字符串中删除单词

时间:2017-06-29 13:19:11

标签: python

我想从字符串中删除单词列表。

例如: 清单是:

["it's","didn't",isn't,"don't"]

字符串是:

'it's a toy,isn't a tool.i don't know anything.'

如何从字符串中删除it'sdidn'tisn'tdon't

2 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点,我将解决2.一个是按字分割字符串,并逐字逐句地与要删除字的字符串进行比较。另一种是扫描每个字符组的字符串。我将举例说明各自的优缺点。

第一种方法是按字词拆分列表。这很好,因为它遍及整个列表,你可以使用列表推导来提取你想要的值,但是,如果写入它只会在空格上分割,所以它会错过任何触及标点符号的东西。 This question解决了如何避免这个问题,以便这个答案可行。

your_string = "it's a toy,isn't a tool.i don't know anything."
removal_list = ["it's","didn't","isn't","don't"]

edit_string_as_list = your_string.split()

final_list = [word for word in edit_string_as_list if word not in removal_list]

final_string = ' '.join(final_list)

第二个选项是按原样删除字符串中这些术语的所有实例。这很好,因为它可以避免标点符号问题,但它确实有一个缺点;如果你删除某些内容并且它是另一个单词的一部分,那么该部分将被删除(例如,如果你有一个带有“sand”字样的字符串并尝试删除“和”它将删除“and”from“沙子“并在字符串中留下”s“。

your_string = "it's a toy,isn't a tool.i don't know anything."
removal_list = ["it's","didn't","isn't","don't"]

for word in removal_list:
    your_string = your_string.replace(word, "")

我希望其中一种解决方案能满足您的需求。

答案 1 :(得分:0)

试试这个:

s = "it's a toy,isn't a tool.i don't know anything."
list = ["it's","didn't","isn't","don't"]

split_line = s.split()
for word in split_line:
    if word in list:
        list.remove(word)
output = ' '.join(list)

注意:这并不能说明单词在不同情况下是否符合标点符号的情况,例如你的toy,isn't