我有绳子
hello
元音必须交换,输出为 holle
e
和 o
被交换
下面是我的代码
vowels = ['a','e','i','o','u']
first_str = 'aiao'
l = list(first_str)
vowel_list = []
for vowel in l :
if vowel in vowels:
vowel_list.append(vowel)
for index,value in enumerate(l):
if value in vowels:
# print(value)
l[index] = vowel_list[-1]
vowel_list.remove(vowel_list[-1])
print(vowel_list)
''.join(l)
我得到了输出
oaai
预期也是oaia
我的方法
编辑礼貌@pranav 使用 pop 代码正在工作 ine
for index,value in enumerate(l):
if value in vowels:
l[index] = vowel_list.pop(-1)
''.join(l)
给你
vowels = "aeiou"
str = 'aiao'
str = list(str)
i = 0 ; j = len(str)-1
while i < j:
while i<j and str[i] not in v:
i += 1
while i<j and str[j] not in v:
j -= 1
str[i], str[j] = str[j], str[i]
i += 1
j -= 1
print("".join(str))
方法:(双指针方法)
希望这个解决方案有帮助