Python 从字符串中删除子字符串

Python 从字符串中删除子字符串

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

删除子字符串的不同方法

替换函数

这是一种非常简单的方法,用于从字符串中删除子字符串。通过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.

翻译功能

我们将在此方法中使用翻译功能来从主字符串中删除子字符串。翻译函数返回指定元素在翻译表中存在的字符串,并将其替换为空字符串。创建翻译表以删除子字符串的代码如下:

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从主字符串中删除子字符串。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程