如何在 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 减 1。
最后,使用 "".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() 函数和字符串连接。这两种方法都有效地识别和反转辅音,同时保留字符串中的元音和其他字符。这些方法提供了灵活性,可以应用于需要辅音反转的各种场景。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP