Python 如何反转字符串辅音而不影响其他元素
字符串辅音指的是发音字符串或字母时产生的非元音声音。在本文中,我们将学习如何使用以下两种方法在Python中仅反转字符串中的辅音而不影响其他元素的位置:
- 使用append()和join()函数
-
使用pop()函数和字符串拼接
示例
Before Reversing : tutorialspoint
After Reversing: tunopiaslroitt
步骤
下面是执行所需任务的算法/步骤:
- 定义函数checkConsonant(char)以检查一个字符是否为辅音字母。如果字符不在元音字母([‘a’, ‘e’, ‘i’, ‘o’, ‘u’])的列表中,则返回True;否则返回False。
-
定义函数consonantReversing(inputString)以反转输入字符串中的辅音字母。
-
使用list(inputString)将输入字符串转换为字符列表。
-
创建一个空列表consonantsList来存储辅音字母。
-
使用for循环遍历字符列表。对于每个字符,调用checkConsonant(char)函数来检查它是否为辅音字母。如果是辅音字母,则将其附加到consonantsList中。
-
将变量l设置为consonantsList的长度减1。
-
开始替换输入字符串中的辅音字母。对于列表中的每个字符,如果它是辅音字母(使用checkConsonant()进行检查),则使用索引l将其替换为consonantsList中的对应辅音字母。然后,l递减。
-
最后,使用””.join(new_list)将修改后的字符列表连接成一个字符串,并将其作为结果返回。
-
通过两次调用不同输入字符串的consonantReversing()函数来演示辅音字母的替换。
使用append()和join()函数
以下代码包括一个名为consonantReversing的函数,它接受一个字符串作为输入,并反转字符串中的辅音字母,同时保持元音字母不变。它通过识别输入字符串中的辅音字母,将它们存储在一个列表中,然后用列表中的辅音字母以相反的顺序替换字符串中的辅音字母来实现这一目标。修改后的字符串将作为输出返回。
示例
# creating a function that checks if the string character
# is consonant or not
def checkConsonant(char):
# list of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
# checking whether the character is not in vowels
if char not in vowels:
# returning true, if the condition is true
return True
# else returning false
return False
# creating another function that reverses the consonants of the string
def consonantReversing(inputString):
# converting the input string into list of characters
new_list = list(inputString)
# creating an empty list for storing consonants
consonantsList = []
# traversing through the above list of characters
for char in new_list:
# checking if the current character is a consonant
# by calling the checkConsonant function
if checkConsonant(char):
# appending that character to the consonants list
# if the condition is true
consonantsList.append(char)
l = len(consonantsList)-1
# Substitute the consonants within a provided string by replacing them with elements
# from a consonant list, starting from the last element in the list.
for m in range(len(new_list)):
if checkConsonant(new_list[m]):
new_list[m] = consonantsList[l]
l -= 1
return "".join(new_list)
# calling the above consonantReversing() function by
# passing the input string as an argument to it.
print(consonantReversing("tutorialspoint"))
# checking for the other string
print(consonantReversing("python codes"))
输出
执行上述程序后,将生成如下输出结果
tunopiaslroitt
sdc onhtoyep
使用pop()函数和字符串拼接
在下面的示例中,另一种方法直接在迭代字符串时识别输入字符串中的辅音,而不使用额外的函数。它将辅音存储在一个列表中,并在第二次迭代过程中以相反的顺序替换它们。其余的字符,包括非辅音字符,将按原样附加到输出字符串中。
示例
def consonantReversing(inputString):
consonants = [c for c in inputString if c.lower() in 'bcdfghjklmnpqrstvwxyz']
outputString = ""
for char in inputString:
if char.lower() in 'bcdfghjklmnpqrstvwxyz':
outputString += consonants.pop()
else:
outputString += char
return outputString
# Example usage:
print(consonantReversing("tutorialspoint"))
print(consonantReversing("python codes"))
输出
tunopiaslroitt
sdcnoh toyep
结论
总之,所提供的Python程序成功地反转了给定字符串中的辅音,而不影响其他元素的位置。它使用了两种不同的方法:一种使用append()和join()函数,另一种使用pop()函数和字符串连接。这两种方法能有效地识别和反转辅音,同时保留字符串中的元音和其他字符。这些方法具有灵活性,可以应用于需要反转辅音的各种场景。