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
列表推導式
透過這種方法,我們將建立一個僅包含所需字元的新列表,在從原始字串中移除所有不需要的字元之後。我們可以透過以下示例清楚地瞭解它
示例
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
結論
程式中存在的字串的編輯是程式設計師遵循的常見過程。因此,瞭解以上所有方法對於成為一名高效快捷的程式設計師至關重要。可以參考本文了解許多從字串中移除字元的不同方法。
廣告