Python 从字符串中删除不需要的字符
Python是一个非常常用的程序,用于不同的目的,如网站开发、数据科学、机器学习,以及执行自动化的不同过程。在任何应用领域工作时,我们都必须处理一个共同的东西,称为字符串。因此,在本文中,我们将学习如何从字符串中删除不需要的字符。
替换
在这种方法中,我们只需指定不需要的元素,然后该元素就会被删除,并在字符串中留下一个空位。我们可以通过以下示例更好地理解它:
示例
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
列表推导
通过这种方法,我们将从原始字符串中删除所有不需要的字符,并仅创建一个包含所需字符的新列表。我们可以通过以下示例清楚地理解:
示例
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
翻译方法
这是一种非常有用的方法,可以从一组字典中移除字符。要移除的字符由翻译表指定,并会相应地被移除。通过使用翻译方法来移除字符的示例如下:
示例
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
过滤函数
正如其名称所示,这种方法中我们将指定一些过滤器来删除字符串中的某些字符。这是从字符串中删除字符的最简单方法之一。我们可以通过以下示例更清楚地理解:
示例
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
结论
对程序中的字符串进行编辑是程序员常见的过程。因此,了解上述提到的所有方法对于成为高效快速的程序员非常必要。读者可以参考本文了解许多不同的方法来从字符串中删除字符。