Python程式:去除字串中長度為K的單詞


在這篇文章中,我們將學習一個Python程式,用於去除字串中長度為K的單詞。

使用的方法

以下是完成此任務的各種方法:

  • 使用split()、join()、len()和列表推導式

  • 使用filter()、lambda()、split()和len()函式

  • 使用split()、join()、remove()和len()函式

示例

假設我們已經輸入了一個字串和長度k。我們現在將使用上述方法從輸入字串中刪除所有給定的長度為k的單詞。

輸入

inputString = "hello tutorialspoint python codes"
k_length = 5

輸出

Resultant string after removing 5 length words:
tutorialspoint python

在這個例子中,給定的k長度為5,因此所有長度為5的單詞,例如hello, codes,都將從輸入字串中刪除,並列印結果字串。

方法1:使用split()、join()、len()和列表推導式

列表推導式

當你希望基於現有列表的值構建新列表時,列表推導式提供了一種更短/更簡潔的語法。

join() − join()是Python中的一個字串函式,用於連線由字串分隔符分隔的序列元素。此函式連線序列元素以轉換為字串。

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入字串並列印給定的字串。

  • 建立另一個變數來儲存長度k

  • 使用split()函式(將字串拆分為列表。我們可以定義分隔符;預設分隔符是任何空格)將輸入字串拆分為單詞列表。

  • 遍歷words列表,檢查當前元素的長度是否不等於輸入的k長度,並將結果儲存在一個列表中。

  • 使用join()函式將列表轉換為字串。

  • 從輸入字串中刪除所有給定的長度為k的單詞後,列印結果字串。

示例

以下程式使用split()、join()、len()函式和列表推導式,返回刪除輸入字串中所有長度為k的單詞後的字串:

# input string
inputString = "hello tutorialspoint python codes"

# printing the input string
print("Input string: ", inputString)

# input k length value
k_length = 5

# splitting the input string into a list of words
wordsList = inputString.split()

# traversing through the words list and checking whether the length of

# current string is not equal to input k length and storing it in a list
resultList = [element for element in wordsList if len(element) != k_length]

# joining the above list to a string to convert it to a string
resultString = ' '.join(resultList)

# printing resultant string after removing k-length words
print("Resultant string after removing 5 length words:\n", resultString)

輸出

執行上述程式後,將生成以下輸出:

Input string: hello tutorialspoint python codes
Resultant string after removing 5 length words:
tutorialspoint python

方法2:使用filter()、lambda()、split()和len()函式

filter()函式 − 使用一個函式來過濾指定的序列,該函式確定序列中每個元素是否為真或假。

Lambda函式

Lambda函式,通常稱為“匿名函式”,與普通的Python函式相同,只是它可以無需名稱進行定義。普通的函式使用def關鍵字定義,而匿名函式使用lambda關鍵字定義。但是,它們僅限於單行表示式。它們與普通函式一樣,可以接受多個引數。

語法

lambda arguments: expression

示例

以下程式使用filter()、lambda()、split()和len()函式,返回刪除輸入字串中所有長度為k的單詞後的字串:

# input string
inputString = "hello tutorialspoint python codes"

# printing the input string
print("Input string: ", inputString)

# input k length value
k_length = 5

# Splitting the input string into a list of words
wordsList = inputString.split()

# Traverse in the given words list and filter the list elements

# if the length of the element is not equal to k using the lambda function.
resultList = filter(lambda element: len(element) != k_length, wordsList)

# Converting filter object to the list
resultList = list(resultList)

#joining the above list to a string to convert it to a string
resultString = ' '.join(resultList)

# printing resultant string after removing k-length words
print("Resultant string after removing 5 length words:\n", resultString)

輸出

執行上述程式後,將生成以下輸出:

Input string: hello tutorialspoint python codes
Resultant string after removing 5 length words:
tutorialspoint python

方法3:使用split()、join()、remove()和len()函式

split() − 將字串拆分為列表。我們可以定義分隔符;預設分隔符是任何空格。

remove()方法 − 刪除具有給定值的元素的第一次出現。

語法

list.remove(obj)

示例

以下程式使用split()、join()、remove()和len()函式,返回刪除輸入字串中所有長度為k的單詞後的字串:

# input string
inputString = "hello tutorialspoint python codes"

# printing the input string
print("Input string: ", inputString)

# input k length value
k_length = 5

# splitting the input string into a list of words
wordsList = inputString.split()

# making a copy of the words list and traveling in that list
for element in wordsList.copy():
   
   # checking whether the length of current is equal to the input k length
   if len(element) == k_length:
      
      # removing that element if the condition is true
      wordsList.remove(element)
      
# joining words list to string to convert to a string
resultString = ' '.join(wordsList)

# printing resultant string after removing k-length words
print("Resultant string after removing 5 length words:\n", resultString)

輸出

執行上述程式後,將生成以下輸出:

Input string: hello tutorialspoint python codes
Resultant string after removing 5 length words:
tutorialspoint python

結論

在這篇文章中,我們學習瞭如何使用三種不同的方法從給定的單詞列表中刪除長度為k的詞。我們還學習瞭如何使用lambda和filter函式根據指定條件過濾列表的元素。

更新於:2023年1月27日

464 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告