如何在 Python 字串中交替排列母音和子音?


單詞由構成字串的母音和子音組成。使用 Python 語言,可以在字串內部改變母音和子音。執行此操作的方法可能多種多樣,一些方法可能是簡單的迴圈迭代,而另一些方法則可能是一些函式和方法。本文將指導使用者學習如何在 Python 字串中交替排列母音和子音。本文描述了三種方法。Python 語言中提供了多種內建函式來解決複雜問題。

讓我們深入探討本文。

方法

方法 1:使用 join() 和 zip() 方法

方法 2:使用 lambda 函式

方法 3:使用 zip_longest() 方法

方法 1:使用 join() 和 zip() 方法交替排列母音和子音的 Python 程式

用於執行字串母音和子音交替排列的函式是 join() 和 zip() 方法。列表推導式迭代列表中的字串。

演算法

  • 步驟 1 − 初始化兩個空列表,一個用於母音,另一個用於子音。

  • 步驟 2 − 使用 for 迴圈從字串中過濾母音和子音

  • 步驟 3 − 然後根據字串中存在的母音和子音,使用切片方法對其進行更改。

  • 步驟 4 − 新字串使用 join() 方法將母音和子音連線在一起。

  • 步驟 5 − 列印語句將返回更改後的字串。

示例

#Empty list is initialized to hold the vowels and consonants
vow_str = []
con_str = []

#the user is prompted to enter the string
str_1 = "Welcome to Paris"
#for loop is used to iterate through the string
for char in str_1:
   #checking whether the character of the string is lower
   if char.isalpha():
      if char.lower() in 'aeiou':
         #append() function is used to add the vowels of the string
         vow_str.append(char)
      else:
         #append() function is used to add the consonants of the string
         con_str.append(char)

#join() method is assigned to the output_str to add both the vowels and consonants
output_str = ''.join([a+b for a,b in zip(con_str,vow_str)])
#print statement returns the strings after altering vowels and consonants
print(output_str)

輸出

WelocemotaPi

方法 2:使用 lambda 函式交替排列母音和子音的 Python 程式

用於執行字串母音和子音交替排列的函式是 join() 和 zip() 方法,以及 lambda 函式的 key 引數。列表推導式迭代列表中的字串。

演算法

  • 步驟 1 − 初始化名為 vowels 的變數。

  • 步驟 2 − 然後根據字串中存在的母音和子音,使用 lambda 函式對其進行更改。

  • 步驟 3 − 在 lambda 函式的幫助下過濾母音和子音。

  • 步驟 4 − 新字串使用 join() 方法將母音和子音連線在一起。

  • 步驟 5 − 列印語句將返回更改後的字串。

示例

#the variable vowel is initialized
vowels = 'aeiou'
#lambda function filters only the vowels from the string
vowel_filter = lambda char: char.lower() in vowels
#lambda function filters only the consonants from the string
consonant_filter = lambda char: char.lower() not in vowels
#the filtered vowels is listed
vow_str = list(filter(vowel_filter, str_1))
#the filtered consonants is listed
con_str = list(filter(consonant_filter, str_1))

#join() method is assigned to the output_str to add both the vowels and consonants
output_str = ''.join([a+b for a,b in zip(con_str,vow_str)])

#print statement returns the strings after altering vowels and consonants
print(output_str)

輸出

Enter the string: Welcome Home
WelocemoHe

方法 3:使用 zip_longest() 函式交替排列母音和子音的 Python 程式

for 迴圈用於查詢母音和子音,然後將它們儲存在分配的變數中。

演算法

  • 步驟 1 − 匯入 itertools 模組以使用 zip_longest() 函式。

  • 步驟 2 − 使用 for 迴圈從字串中過濾母音和子音

  • 步驟 3 − 然後根據字串中存在的母音和子音對其進行過濾。

  • 步驟 4 − 新字串使用 join() 方法將母音和子音連線在一起。

  • 步驟 5 − 列印語句將返回更改後的字串。

示例

#importing the itertools function to use the zip_longest function
from itertools import zip_longest

#the user is prompted to enter the string
str_1 = "Welcome home "
#the variable vowel is initialized
vowels = 'aeiou'

#vowel and consonants are assigned to get it from the string
vow_str = [char for char in str_1 if char.lower() in vowels]
con_str = [char for char in str_1 if char.lower() not in vowels]

#join() method is assigned to the output_str to add both the vowels and consonants
output_str = ''.join([a+b for a,b in zip_longest(con_str,vow_str, fillvalue='')])

#print statement returns the strings after altering vowels and consonants
print(output_str)

輸出

Welocemo ehm

結論

Python 語言屬於 OOPS 概念,它會立即執行程式碼而無需檢查錯誤。與其他語言相比,它因其優勢而佔據獨特的地位,例如易於使用簡單的語法和語句進行編碼。

更新於: 2023-08-25

160 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.