使用 Python 从字符串中移除子字符串列表


Python 是一种非常有用的软件,被世界各地的人们广泛使用,以根据其个人需求执行许多不同的功能。它用于许多不同的目的,例如数据科学、机器学习、Web 开发以及执行不同的自动化流程。它具有许多不同的功能,可以帮助我们执行上述任务,但由于 Python 中存在如此多的功能,用户也必须面对问题。用户面临的一个常见问题是从字符串中移除子字符串。很多时候,还需要从一个主字符串中移除多个子字符串。在本文中,我们将学习如何使用 Python 从字符串中移除子字符串列表。

移除子字符串的不同方法

replace 函数

这是一种从字符串中移除子字符串的非常简单的方法。借助 replace() 函数,只需定义要保留的字符串和要移除的子字符串,我们就可以轻松地移除不需要的子字符串。让我们举个例子来说明: -

def extra_substrings(main_string, remove_substrings): #Defining the format of string and substring
    for remove_substring in remove_substrings:
        main_string = main_string.replace(remove_substring, "")  #For any substring having substring within it
    return main_string

示例

def extra_substrings(main_string, remove_substrings): #Defining the format of string and substring
    for remove_substring in remove_substrings:
        main_string = main_string.replace(remove_substring, "")  #For any substring having substring within it
    return main_string
whole_string = "Hello, everyone! This is a extra string just for example."
remove_substrings = ["everyone", "extra", "just"]  #These are the substrings which are to be removed with the help of replace() function
new_String = extra_substrings(whole_string, remove_substrings) #The extra_substring checks each of the defined substrings and removes them from the string
print(new_String)

输出

上述代码的输出如下

Hello, ! This is a  string  for example.  

re 模块

在此过程中,将使用 re 模块从主文本中提取子字符串。Python 使用 re 模块来处理正则表达式。为了定义子字符串并将其从字符串中删除,我们将使用 re 模块的 re.sub() 方法设计一个模式。此方法的代码如下所示

import re  #Do not forget to import re or else the code will not run correctly

def extra_substrings(main_string,remove_substrings):  #The string and substring are taken as argument by extra_substring
    pattern = "|".join(map(re.escape, remove_substrings)) #The | will act as a separator in the pattern that is defined
    return re.sub(pattern, "", main_string)   # The re.sub() function will be used to replace all the substring in the pattern with an empty place

示例

让我们举个上述代码的例子,使其更清晰

import re  #Do not forget to import re or else the code will not run correctly

def extra_substrings(main_string,remove_substrings):  #The string and substring are taken as argument by extra_substring
    pattern = "|".join(map(re.escape, remove_substrings)) #The | will act as a separator in the pattern that is defined
    return re.sub(pattern, "", main_string)   # The re.sub() function will be used to replace all the substring in the pattern with an empty place
whole_string = "Hello, everyone! This is a extra string just for example."
remove_substrings = ["everyone", "extra", "just"]

new_string = extra_substrings(whole_string, remove_substrings)  #The argument will remove all the words defined within substrings
print(new_string)

输出

上述代码的输出如下

Hello, ! This is a string for example.

列表推导式

这是另一种从主字符串中移除子字符串的极其简单的方法。在定义子字符串之前,我们可以向函数提供字符串和子字符串参数。列表推导式将检查主文本的每个组件,并删除代码中找到的任何子字符串。此方法的代码如下所示

def extra_substrings(main_string, remove_substrings): 
    words = main_string.split()  # Split the string into words
    useful_words = [word for word in words if all(sub not in word for sub in remove_substrings)]   #With the help of all() function, list comprehension will check all the elements in the string and all the defined substrings will be removed
    return ' '.join(useful_words)

示例

让我们举个使用上述代码的例子,使其更清晰

def extra_substrings(main_string, remove_substrings): 
    words = main_string.split()  # Split the string into words
    useful_words = [word for word in words if all(sub not in word for sub in remove_substrings)]   #With the help of all() function, list comprehension will check all the elements in the string and all the defined substrings will be removed
    return ' '.join(useful_words)
whole_string = "Hello, everyone! This is a extra string just for example."
remove_substring = ["everyone", "extra", "just"]

new_string = extra_substrings(whole_string, remove_substring)
print(new_string)

输出

上述代码的输出如下

Hello, ! This is a string for example.

translate 函数

在此方法中,我们将使用 translate 函数从主字符串中移除子字符串。translate 函数返回包含翻译表中指定的元素的字符串,并将它们替换为空字符串。创建翻译表以移除子字符串的代码如下所示

def remove_substrings_translate(main_string, remove_substrings):
    translation_table = str.maketrans("", "", "".join(remove_substrings)) #str.maketrans() is used to create the translational table 
    return main_string.translate(translation_table)  #str.translate() is used to remove the substrings with the help of translational table

示例

让我们举个使用上述代码的例子,以便更清楚地理解它

def remove_substrings_translate(main_string, remove_substrings):
    translation_table = str.maketrans("", "", "".join(remove_substrings)) #str.maketrans() is used to create the translational table 
    return main_string.translate(translation_table)  #str.translate() is used to remove the substrings with the help of translational table
whole_string = "Hello, world! This is a sample string."
remove_substrings = ["world", "sample"]
new_string = remove_substrings_translate(whole_string, remove_substrings)
print(new_string)

输出

上述代码的输出如下

H, ! Thi i   ting.

re 模块与函数结合使用

这是一种复杂的方法,用于用户需要更多灵活性的情况。我们将使用 re.sub() 函数并创建一个另一个个性化的自定义函数,该函数允许我们决定要替换的子字符串。结合使用 re.sub() 和自定义函数的代码如下所示

import re #Do not forget to import re or else error might occur

def extra_substrings(main_string, remove_substrings):  #Defining the arguments
    pattern = "|".join(map(re.escape, remove_substrings))
    
    def replacement(match): #Custom Function to define the substring with an empty string
        return ""
    
    return re.sub(pattern, replacement, main_string)  #re.sub() to remove the substring defined by custom function replacement()

示例

让我们举个使用上述代码的例子,以便更清楚地理解它

import re #Do not forget to import re or else error might occur

def extra_substrings(main_string, remove_substrings):  #Defining the arguments
    pattern = "|".join(map(re.escape, remove_substrings))
    
    def replacement(match): #Custom Function to define the substring with an empty string
        return ""
    
    return re.sub(pattern, replacement, main_string)  #re.sub() to remove the substring defined by custom function replacement()
whole_string = "Hello, everyone! This is a extra string just for example."
remove_substrings = ["everyone", "extra", "just"]

new_string = extra_substrings(whole_string, remove_substrings)
print(new_string)

输出

上述代码的输出如下

Hello, ! This is a string for example.

结论

如果用户没有采用正确的方法,那么从字符串中移除子字符串的过程可能会变得令人沮丧。这是用户经常遇到的一个问题,因此必须遵循正确的步骤。用户可以参考本文中提供的不同方法,使用 Python 从主字符串中移除子字符串。

更新于: 2023年8月1日

152 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告