Python - 从字符串中移除不需要的字符
Python 是一种非常常用的程序,用于各种目的,例如 Web 开发、数据科学、机器学习,以及执行各种自动化流程。在任何应用领域工作时,我们都必须处理一个共同的东西,即字符串。因此,在本文中,我们将学习如何从字符串中移除不需要的字符。
替换
在这种方法中,我们只需指定不需要的元素,然后移除该元素,并在字符串中留下一个空位。我们可以通过以下示例更好地理解它
示例
def unwanted_string_words(data, sentence): #Input is provided for sent in sentence: #Check different characters in string data = data.replace(sent, '') #Remove unwanted characters from string return data # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
输出
以上代码的输出如下所示
Hi My Name Is John
re 模块
在这种方法中,我们将使用正则表达式模块从字符串中移除字符。我们可以从以下示例中了解其用法
示例
import re # Do not forget to import the re module or else error might occur def unwanted_string_words(data, sentence): # The input is provided pattern = "[" + re.escape("".join(sentence)) + "]" #We will join all the characters using re.escape return re.sub(pattern, "", data) # We will then use re.sub to remove the unwanted characters # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
输出
以上示例的输出如下所示
Hi My Name Is John
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
列表推导式
通过这种方法,我们将创建一个仅包含所需字符的新列表,在从原始字符串中移除所有不需要的字符之后。我们可以通过以下示例清楚地了解它
示例
def unwanted_string_words(data, sentence): #The input is provided return ''.join([sent for sent in data if sent not in sentence]) # All the characters in the string are checked and the characters which are not to be removed sre moved to a new list and some characters are removed. # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
输出
以上示例的输出如下所示
Hi My Name Is John
translate 方法
这是一种非常有用的方法,可以从字典列表中移除字符。要移除的字符在转换表中指定,并相应地移除。以下是如何使用 translate 方法移除字符的示例
示例
def unwanted_string_words(data, sentence): #The input of string is provided translation_table_characters = str.maketrans('', '', ''.join(sentence)) # With the help pf str.maketrans a translation table is made and the characters to be removed are specified return data.translate(translation_table_characters) #The str.translate is used to remove the characters and provide an empty space # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
输出
以上示例的输出如下所示
Hi My Name Is John
filter 函数
顾名思义,在这种方法中,我们将指定一些过滤器以从我们的字符串中移除一些字符。这是从字符串中移除字符的最简单方法之一。我们可以通过以下示例更清楚地了解它
示例
def unwanted_string_words(data, sentence): # The input of string is given unwanted_string_words = filter(lambda sent: sent not in sentence, data) #Filter will check all the characters of the string # Lambda will be used to look for the unwanted characters in the whole string return ''.join(unwanted_string_words) #After removing the unwanted characters, the remaining characters are moved to a new list # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
输出
以上示例的输出如下所示
Hi My Name Is John
列表 & 连接
在这种方法中,整个字符串被转换为字符,然后检查所有字符,并从列表中移除所有不需要的字符。我们可以通过以下示例更好地理解它
示例
def unwanted_string_words(data, sentence): # The input of string is provided unwanted_string_words = [sent for sent in data if sent not in sentence] # List comprehension will be used to check each characters and the unwanted ones will be removed return ''.join(unwanted_string_words) #The remaining characters will be joined to form a string # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
输出
以上示例的输出如下所示
Hi My Name Is John
结论
程序中存在的字符串的编辑是程序员遵循的常见过程。因此,了解以上所有方法对于成为一名高效快捷的程序员至关重要。可以参考本文了解许多从字符串中移除字符的不同方法。