Python程式:替換除指定單詞外的所有單詞
在本文中,我們將學習如何在Python中替換字串中除指定單詞外的所有單詞。
使用的方法
以下是完成此任務的各種方法:
使用For迴圈、split()和join()函式
使用列表推導式
示例
假設我們已經獲取了**輸入字串**、輸入單詞和要替換的輸入字元。我們現在將使用上述方法,將輸入字串中的所有單詞替換為給定的輸入替換字元,但指定的單詞除外。
輸入
inputString = 'hello tutorialspoint python codes' inputWord = "tutorialspoint" replaceChar = "@"
輸出
Replacing all words of string except input word with '@': @ tutorialspoint @ @
在這個例子中,給定的輸入單詞是“**tutorialspoint**”。因此,輸入字串中的所有字元都將被替換為**“@”**符號,但“tutorialspoint”這個單詞除外。
方法1:使用For迴圈、split()和join()函式
**join()** − join()是Python中的一個字串函式,用於連線由字串分隔符分隔的序列元素。此函式連線序列元素以轉換為字串。
**split()**函式 − 將字串拆分為列表。我們可以定義分隔符;預設分隔符是任何空格。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存**輸入字串**。
列印輸入字串。
建立一個變數來儲存**輸入單詞**。
給出**替換字元**(將要替換的字元)。
使用**split()**函式將輸入字串拆分為單詞列表。
使用for迴圈遍歷單詞列表的長度,使用**len()**函式(返回物件中的專案數)。
獲取當前索引處的列表元素。
使用**if條件**語句檢查當前元素是否不等於輸入單詞(此處為tutorialspoint)。
如果條件為真,則將當前元素替換為給定的替換字元。
使用**join()函式**將給定的單詞列表轉換為字串。
列印替換輸入字串中除輸入單詞外的所有單詞後得到的字串(使用輸入替換字元)。
示例
下面的程式使用for迴圈、split()和join()函式,將輸入字串中除指定單詞外的所有單詞替換為輸入替換字元(@)。
# input string
inputString = 'hello tutorialspoint python codes'
# printing input string
print("Input String:", inputString)
# input word
inputWord = "tutorialspoint"
# input replacing character (with which to be replaced)
replaceChar = "@"
# splitting input string into the list of words
wordsList = inputString.split(" ")
# traversing till the length of the words list
for index in range(len(wordsList)):
# getting the list element at current index
element = wordsList[index]
# checking whether the current element is not equal to the input word(tutorialspoint)
if not element == inputWord:
# assigning the input replacing character to the current element
# if the condition is true
wordsList[index] = replaceChar
# converting words list to a string using the join() function
resultantString = " ".join(wordsList)
# printing resultant string after replacing all words of the string
# except input word with input replacing character
print("Replacing all words of string except input word with '@':\n", resultantString)
輸出
執行上述程式後,將生成以下輸出:
Input String: hello tutorialspoint python codes Replacing all words of string except input word with '@': @ tutorialspoint @ @
**時間複雜度** − O(n)
**輔助空間** − O(n)
方法2:使用列表推導式
列表推導式
當您希望根據現有列表的值構建新列表時,列表推導式提供了一種更短/更簡潔的語法。
這是完成此任務的另一種方法。在此方法中,我們迭代元素並使用與先前方法具有類似功能的單行程式碼執行任務。
示例
下面的程式使用列表推導式,將輸入字串中除指定單詞外的所有單詞替換為輸入替換字元(*)。
# input string
inputString = 'hello tutorialspoint python codes'
# printing input string
print("Input String:", inputString)
# input word
inputWord = "python"
# input replacing character (with which to be replaced)
replaceChar = "*"
# Here we create a new list using list comprehension
# Where if the element(word) is equal to the input word then we add a word to the list
# Else we add replace character to the list
resultantList = [replaceChar if not element == inputWord else element for element in inputString.split()]
# Join the list of words with spaces to make it a string
resultantString = " ".join(resultantList)
# printing resultant string after replacing all words of string
# except input word with input replacing character
print("Replacing all words of string except input word with '*':\n", resultantString)
輸出
執行上述程式後,將生成以下輸出:
Input String: hello tutorialspoint python codes Replacing all words of string except input word with '*': * * python *
**時間複雜度** − O(n)
**輔助空間** − O(n)
結論
在本文中,我們研究了兩種不同的方法來替換除指定單詞外的所有單詞。我們還學習瞭如何使用列表推導式以簡單簡潔的語法來完成任何任務。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP